Why Are My PHP Files Showing as Plain Text

Why are my PHP files showing as plain text?

You'll need to add this to your server configuration:

AddType application/x-httpd-php .php

That is assuming you have installed PHP properly, which may not be the case since it doesn't work where it normally would immediately after installing.

It is entirely possible that you'll also have to add the php .so/.dll file to your Apache configuration using a LoadModule directive (usually in httpd.conf).

How do I prevent PHP displaying as plain text on my browser?

The title of your post is wrong. It should be something like "How to prevent My php code from displaying on the screen/browser".

Below are possible scenario.

If your PHP code is being shown in the browser, it implies that your server has not been setup to serve PHP scripts. Below are among few things you will need to do.

1.)PHP Xampp or Wamp Installation:. I prefer Xampp. First step is to ensure that PHP is installed and running correctly. You can download and Install Xampp if you have not done so.
An easy way to check if php is installed is to run php -v from a command line and see if returns version information or any errors

2.) Restarting your Server: If you have alter any files prior to this event, you will need to restart your server

3.) PHP File Extension Name: Ensure that you properly save your code as .php file extension name. Code save as .html or .txt will not be executable.

4.) In case if you are using Xampp. Ensure that your php files resides on htdocs folder if you are using xampp Eg

C:\xampp\htdocs\your-php-projects.

5.)Misconfiguration: This may be the last thing to check. But if you care about it, You can also check for misconfigurations.

For example: In Apache’s httpd.conf file, you will need to make sure that the line Eg as case may be "LoadModule php5_module" has been uncommented and that there is no semi-colon (;) at the beginning of the line.

screenshot

PHP showing up as plain text

Here are possible answers to your problem:

  1. WEB server is not processing your PHP code correctly because php is not installed or misconfigured.
  2. You're not using the web server at all. Make sure to load your pages from localhost (or whatever your server is) and not from the file itself
    ex: localhost/path/to/index.html
    and not c://program files...

PHP code is shown in browser as plain-text and not processed

Okay, I figured it out. I believe the problem stemmed from my own lack of understanding of OS X in general and Apache/MAMP in particular (I'm new to both!). Specifically, I didn't realize that OS X runs its own Apache server, and that when you install MAMP, it essentially gives you an alternate installation (I think?). I realized this when I tried to start Apache from the terminal using 'sudo apachectl -k start' and it said process was already running. I then did a bit of Googling and understood why.

Then I implemented the solution. Previously, I had everything under User\Sites and I was getting the PHP script back in plain-text. I moved my files to Applications/MAMP/htdocs. Then I ran MAMP PRO, which showed that Apache was running on port 8888. So I navigated to localhost:8888/~username/ and submitted the form, and the PHP script was processed correctly and I got the echo back.

Someone correct me if my understanding is incorrect. But at least my little project works now. :)

Why is my PHP file class recognized as text file?

  1. Settings (Preferences on macOS) | Editor | File Types
  2. Locate "PHP" entry in the top list (Recognized File Types)
  3. Ensure that the middle list (File Name Patterns) has *.php entry there.

Sample Image

If it's not there then add it. Looks like you somehow re-assigned it to the "Text" file type.

Sample Image

Sample Image



Altought blade.php get recognized right,

That's because *.blade.php is assigned to separate/dedicated "Blade" file type (so IDE can provide Blade-specific support there).

PHP code is not being executed, but the code shows in the browser source code

Sounds like there is something wrong with your configuration, here are a few things you can check:

  1. Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php -v from a command line and see if returns version information or any errors.

  2. Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule php5_module "c:/php/php5apache2_2.dll" in the file. Search for LoadModule php, and make sure that there is no comment (;) in front of it.

  3. Make sure that Apache's httpd.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.

  4. Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP.

  5. Make sure you are not using short tags in the PHP file (<?), these are not enabled on all servers by default and their use is discouraged. Use <?php instead (or enable short tags in your php.ini with short_open_tag=On if you have code that relies on them).

  6. Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php

And lastly check the PHP manual for further setup tips.

How to show uploaded php file as plain text instead of executing it in wordpress?

You already have the right code — AddType text/plain php, which will make Apache treats PHP files (or files where the name ends with .php) as plain-text files.

But assuming the following:

  • You have WordPress installed in the /var/www/html directory.

  • The PHP files are uploaded to the default uploads folder in WordPress (wp-content/uploads).

If you set the directory to /var/www/html/wp-content/uploads as in:

<Directory /var/www/html/wp-content/uploads>
AddType text/plain php
</Directory>

You'd get the results you wanted — only PHP files in the uploads folder will be treated as plain-text files, and not all PHP files in the /var/www/html directory. This explains the issue with "To open the post http://home.local/wp/?p=4785, I got the following output", where that output is the code in the file /var/www/html/index.php. I mean, you used <Directory /var/www/html>, which makes Apache treats the index.php file as a plain-text file, instead of executing the PHP code in the file.

ALTERNATE METHOD: Use the .htaccess file.

Particularly if you can't edit or have no access to the Apache's configuration (.conf) file.

  1. Create .htaccess in the uploads folder, if it's not already there.

  2. Then add AddType text/plain php in that file.

Additional Notes

I want to see the content in testme.php, click it, pop up new window

I'm sure you can do that or already have the code/solution, but an easy way, is just add target="_blank" to the attachment/file link.. or with JavaScript, you can use window.open().

And you must take full security measures since allowing people to upload PHP files could harm your site and/or your site users.



Related Topics



Leave a reply



Submit