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 to show php-files as plain text in Apache
THE ANSWER:
in .htaccess-file type
php_flag engine off
#This will prevent apache from executing *.php-files
AddType text/plain php
#this wil display php-files in browser (if not, browser will want to download file!)
Thanks to Brad!
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.
Create
.htaccess
in theuploads
folder, if it's not already there.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.
PHP code is rendered as text
You didn't install apache properly, doing an apt-get on apache2 does not install everything.
what @newman stated is correct you can follow that guide, or here is a digitalocean link that is usuable for production server (since you would do this on a droplet). Note this is full stack LAMP, which I would assume you would get to eventually when you want to dab with mysql
https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04
Apache shows PHP code instead of executing it
You must enable php! Check the folder mods-enabled
in the Apache directory (default: /etc/apache2/
) to see if you find a file named php
. I don't remember the extension but I think it's .so
.
Also check in /var/log/apache2/error.log
to see if you have any other errors.
php displaying as plain text
Use command php -v
and if does'nt show you php version then you have to install PHP. Do
yum install php
yum install php-pear
then
sudo service httpd restart
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. :)
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.
PHP Files shown as Plaintext in Apache2 LAMP Machine
RIP GUYS.
I just show it. I set the header to text/plain
.
If you remove the header all works fine:
header('Content-type: text/plain; charset=utf-8');
..or if we change it to text/html
.
Related Topics
Differencebetween Null and Empty
Message: Trying to Access Array Offset on Value of Type Null
How to Detect Non-Ascii Characters in a String
Reaching 100% Code Coverage with PHPunit
Get Start and End Days for a Given Week in PHP
How to Get the Last Path in a Url
PHP & MySQL: When Exactly to Use HTMLentities
PHP - How to Implement Password Reset and Token Expiry
Php: Catch Exception and Continue Execution, Is It Possible
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
Symfony2: How to Access Service from Template
How to Prevent PHP Files from Being Downloaded? and What Are Some Ways Someone Can Download Them
PHP Get the Last 3 Elements of an Associative Array While Preserving the Keys
Downloading a Folder Through with Ftp Using PHP
PHP Hsv to Rgb Formula Comprehension