Why does my php file download instead of executing?

I have a local LAMP server set up on my Ubuntu laptop for testing various PHP scripts.

Sometimes, when I do something wrong, instead of getting an error, the script I'm testing is offered for download. Why is that and how can it be fixed?

asked Jun 13, 2009 at 13:20

The server does not recognize the script as something to execute locally, and as such it offers it as a file instead.

These lines should be present in your Apache config. Note that the path may need to be changed for the .so modules, depending on your configuration:

# -- if you're using PHP 5, uncomment this line to activate it
#LoadModule php5_module libexec/libphp5.so

# -- if you're using PHP 4, uncomment this line to activate it
#LoadModule php4_module libexec/libphp4.so

AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

Follow up with a restart of the Apache service and php should load into Apache to run just fine.

answered Jun 13, 2009 at 13:44

Avery PayneAvery Payne

14.3k1 gold badge48 silver badges87 bronze badges

1

Some reasons I have found that this happens:

  • if php is not configured correctly on the server
  • if the file doesn't have the correct .php extension
  • if it is in a directory that does not allow execution
  • if the script takes too long to execute.

One test I use is to make a file in the same directory (verify.php) that contains:

<?php
  phpinfo();
?>

If that file executes in my browser, it the first 3 points are ok

answered Jun 13, 2009 at 14:04

Brent Brent

22.2k19 gold badges68 silver badges102 bronze badges

3

I would agree with Avery, that's the usual cause. However, you specifically say it's only sometimes. Any chance you are changing the content type header?

answered Jun 13, 2009 at 13:50

KyleKyle

1,8492 gold badges17 silver badges23 bronze badges

1

A usual error is to print something to the page before the headers get sent.

answered Jun 13, 2009 at 13:55

NifleNifle

3621 gold badge8 silver badges22 bronze badges

0

Also note that if you have a .htaccess file in the same folder as your php script, it could be the reason for some execution problems.

For example, if you got a copy of the site files from another server, your htaccess file could have a line like:

AddHandler x-httpd-php5-3 .php

But in your actual server you may be running another version of PHP. So it would not excecute right.

answered Mar 10, 2013 at 8:03

  1. Community Forum
  2. Community Support
  3. Server Management
  4. Server Analytics
  5. Questions

PHP files downloading instead of executing.

Why does my php file download instead of executing?

My account was recently downgraded from a dedicated account to a VPS account. I've just discovered that the primary domain on my account (circa75.co) is having issues with with PHP files. Instead of PHP files executing, they are downloading. I've uploaded a test index.php file for testing purposes.

Duplicates 1

index.php downloading instead of opening in browser

My website is working fine, apart from the fact that when ever I try to open domain/rss instead of the browser opening the feed, it tries to download it.


I tried fixing it by altering .htaccess, but that didn't seem to work.


Has something changed on the server side that would account for this problem?


Chris

Confirm

Why is website downloading instead of opening in browser?

There are tens of reasons which can cause this issue of downloading files instead of opening in browser. But mainly you get this issue due to poor hosting provider, any deflect in cache plugin you're using on your website, or you messed up with the . htaccess file.

Why PHP script is not running in browser?

PHP is not intended for execution in a browser. It is for web servers to execute, or other preprocessing on the PHP-installed computer. PHP runs in several incarnations when installed on a computer: from the command line.

How do you execute a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

Do PHP files need to be executable?

php with a shebang line it will need to be executable. If you specify the interpreter (ie. php) it's the interpreter that needs to be able to execute code, so in this case it only needs to be able to read the code.