PHP file downloaded instead of running [duplicate] - php

When I attempt to navigate to the PHP script at http://localhost/project/admin, I expect to see the script results in my web browser.
Instead, I see a dialog box for downloading the file with the message below:
You have chosen to open <filename>, which is a: application/x-httpd-php from: http://localhost
What should Firefox do with this file? Open with... &bullet; Save... &bullet; etc.
Any idea what is going on?

PHP is not correctly installed on the server or Apache is not using mod_php
In the case of this happening in IIS. I would say that the mimetype is not setup correctly and that the server doesn't know how to handle the extension ".php"

It sounds like an incorrect Content-type header is being sent. This header sets the mime-type for the data the browser receives and, if the browser doesn't have a handler to render that specific mime-type, it will ask to open/save the file instead.
I would start by checking your PHP file for a header() statement and a mis-typed content-type.

Related

Header function asks the user for the file to be opened rather than automatically?

I am trying to redirect the user to simply another phtml file when they logout. I am using the function header like so:
header('Location: ../../Site/index.phtml');
exit;
In reality when that runs, the user is greeted if they want the file to be opened and they're not automatically re-directed. Like shown in the image. How can I get them to redirected please without the dialog (automatically). I am running a localhost.
Many thanks
Image of the output
The problem is likely due to the fact that .phtml is a very old PHP file extension (from PHP 2, circa 1997). We're now on PHP 7, and as such, most web servers don't even have logic built in to handle the .phtml extension.
Because your webserver (in this case localhost) doesn't know what to do with the file, it falls back and lets your browser handle it. Because your browser doesn't have an associated default file handler for the extension, it prompts you for an action (assuming a user would be more familiar with a file extension).
The easiest way to resolve this would be to simply switch to the .php extension, as I'm yet to encounter a server that is unable to handle the main PHP extension.
If you are still having issues opening the file, ensure that you specify the type of the file with header("Content-Type: text/html"), which is the default for PHP. This shouldn't be necessary, but will ensure that your browser correctly parses the file as a PHP file.
Hope this helps! :)

Why browser is trying to download my php script file?

Just a quick one - I wrote a php script recently that dynamically creates XML file using API DOM. So I'm using this at the beginning:
$dom = new DOMDocument('1.0', 'UTF-8');
And at the end it looks like this:
$server = $_SERVER['DOCUMENT_ROOT'];
$path_to_xml = "$server/project/file.xml";
file_put_contents($path_to_xml, $dom->saveXML());
It does everything I wanted but why browser is trying to download this php script instead of just run it? Please can someone help me with this. I'm pretty sure it's something easy.
//-----------------------------------edited
Thanks for all replies. Yes I'm sending custom headers because it's google maps kml file that I'm creating dynamically.
header('Content-type: application/vnd.google-earth.kml');
// Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://earth.google.com/kml/2.0', 'kml');
$parNode = $dom->appendChild($node);
Could that be possible cause of this?
Because your web server is not correctly configured to process PHP scripts.
I have experienced that it happens due to some error in the .htaccess, it may seem absurd because i did not mentioned anything to see in the .htaccess. You should take a closer look in to it.
If the browser is trying to download the PHP source, then this means that Apache was either not configured to run the PHP interpreter and/or, if you are using a Linux, Unix, or Mac OS X operating system, Apache did not have permission to execute the PHP script.
You need to follow the instructions at Manually Configure PHP5 and Apache2 to make sure that your httpd.conf is correct.
If, in addition, you are running Apache on Linux, Unix, or Mac OS X, then you need to open a terminal, cd into the directory that contains your PHP script, and:
chmod a+x SCRIPT.php
where SCRIPT.php is the name of your PHP script.
I'm assuming that this command sends a header:
header('Content-type: application/vnd.google-earth.kml');
If you're writing a file on the server and not intending to send any response to the client, why are you sending a header to the client?
If your PHP file is just supposed to write a file on the server and do nothing else, don't send a header or indeed anything else to the client.
If that doesn't help, try rephrasing your question. You have received a wide variety of responses so far to all manner of different problems.
If the file extension is .php and your web server correctly configured it will run it.
You specify an application/xxx content type so most browsers will force a download and use the name of your script as file name.
If you want to force a file name different from your php file name use :
header('Content-Disposition: attachment; filename=your_requested_file.kml');
You might not be sending correct headers:
<?php header ("content-type: text/xml");
My guess is that the browser is not trying to download the PHP script, but IS trying to download the KML file. Comment out the header() line and see if it works. When saving a file locally, you should not need to include header().

index.php is not opening and running as download the file

When I attempt to navigate to the PHP script at http://localhost/project/admin, I expect to see the script results in my web browser.
Instead, I see a dialog box for downloading the file with the message below:
You have chosen to open <filename>, which is a: application/x-httpd-php from: http://localhost
What should Firefox do with this file? Open with... &bullet; Save... &bullet; etc.
Any idea what is going on?
PHP is not correctly installed on the server or Apache is not using mod_php
In the case of this happening in IIS. I would say that the mimetype is not setup correctly and that the server doesn't know how to handle the extension ".php"
It sounds like an incorrect Content-type header is being sent. This header sets the mime-type for the data the browser receives and, if the browser doesn't have a handler to render that specific mime-type, it will ask to open/save the file instead.
I would start by checking your PHP file for a header() statement and a mis-typed content-type.

Logging dynamically served files in APACHE

I'm serving up Zip and PDF files on the fly via PHP using an output such as:
header('Content-Disposition: attachment; filename="'.$project->name .'.zip"');
echo($zipfile->zl_pack());
I can't find any reference to these downloads in my APACHE logs though. Is it not logged due to being dynamic?
Do I need to actually write the file to the webserver and then serve the result up to get it logged or am I missing something?
Cheers,
Niggles
Correct. httpd does not look at outgoing headers for logging. error_log() will send a message to httpd's error log, but there's no way to put something in the access log.
The request to the PHP program that generates that header should be logged. The filename mentioned in the content disposition header won't be.
I believe mod_perl would allow you to add custom logging, I don't know if mod_php provides a similar feature.
As a workaround you could use mod_rewrite to have the *.zip file logged and still served it through PHP without actually writing it to the filesystem, the client will have to send two requests though.
1) Change your current script so that it doesn't produce the file, but instead puts the parameters needed for the file creation in the session; instead of the current header and file content you would put header('Location: '.$project->name .'.zip');
2) This would cause the second request. Since the requested file doesn't exist yet, you would use mod_rewrite to change the request to a *.zip file to the same or some other PHP script that reads the parameters from the session and produces the file just like you're doing it now. If you use the same PHP script, you would additionally let mod_rewrite add some parameter like "?action=produceFile" to the request and then test for that parameter in the script.
This way you would have two nice entries in your Apache log without having to physically save the file (and probably delete it later).
FYI I found a really good work-around.
Part of the problem was that we wanted to force a "save as" dialogue as many of the users get confused as to where the file gets saved. So this is why I went the
Content-Disposition : attachment
method.
A better method which still invokes the dialogue is to add this to .htaccess
<Files *.zip>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
write the Zip to the fileserver and redirect the page to the zip.
Sure I have to cleanup every night, but it gets logged and it still forces them to choose where they download the file (on pretty much everything but Safari [ there's always one ]).
Cheers,
Niggles

Renaming File on another server as user downloads it [2] - using PHP

I have asked this question today already but this time I want to know if I can achieve this via PHP since Javascript wasn't up to it.
I have a link to a file on another server. If i provide this link to my users the headers are pushed out to download that file from that server.
Is there a way for me to capture those headers and file and redirect the download to the user? I would like to do this so that I can change the filename of the download since it is
always 'file.zip'.
Is this possible with PHP?
Thank you for any help.
You can download the file to your server using curl and serve it correctly(with a Content-Disposition header). As long as you are using HTTP, there's no way to send just the header and let another server stream the content directly to the client.
You could do this, and you can do it in several ways.
1) (simple) copy the file to your server, and rename it. Point your download links to this copy.
2) (harder) Create a stub php file, called , read the file from the remote server within php, and stream the content to the script output. This will need you to set appropriate headers, etc. as well as setting up your webserver to parse through PHP.
Seriously, I'd go with option 1. (assumes you have a legal right to serve the content, etc.)
Maybe you can use a script similar to the following one:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/the_path/file.zip");
header('Content-Disposition: attachment; filename="alternate_filename.zip"');
exit();
?>

Categories