Why browser is trying to download my php script file? - php

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().

Related

PHP file downloaded instead of running [duplicate]

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.

Download KML File Instead of View in Browser

I am working on a project which creates a KML File (just like an XML file, but used for Google Earth). Whats interesting is when I link to the newly created file, on my local machine, running XAMPP, the file is downloaded automatically, however when I move it to my web server (Linux, Fedora 8 on EC2) the link just loads the KML file in the browser as if it was an HTML file.
How can I force it to download the file instead of viewing it in the browser?
Here's how to link is displayed with PHP,
echo "<a href='$currentTime.kml'><img heigth=\"15px\" width=\"13px\" src=\"images/KML_Icon.gif\" /> Download</a>";
Any advice would help, thanks!
What you need to do is to specify the headers so the Browser knows what to do with the information that you are sending. So before you send anything to the browser you will need to specify the headers.
If you are linking to a specific file, then you will have to create a little "download manager" that will do this for you.
<?
header('Content-disposition: attachment; filename=the-name-you-want-them-to-see-in-their-download.pdf');
header('Content-type: text/xml'); //Since KML files are based on XML this is probably the best Content type to send to the user.
readfile('the-file-you-want-to-present')
?>
That should do it.
Thank you for your guys' input, but Oded had the answer regarding the mime types.
On the server there's a file called mime.types which didn't contain the mime type for a KML file, I added in
application/vnd.google-earth.kml+xml
And it now downloads the file instead of loading it in the browser, by the way apache needs to be restarted once you have made the changes.
I had this a long while ago, I used a method similar to this:
http://webdesign.about.com/od/php/ht/force_download.htm

How to use google docs viewer for embedding ppt files on a webpage, if the file is served by a php script?

I've uploaded an MS powerpoint file to my server, and I'm trying to use Google Docs viewer (http://docs.google.com/viewer) to display it on a webpage.
The file is available here:
http://elgg.wamped.org/test.ppt
If I invoke the docs viewer with the above URL, it is working as expected, see:
http://docs.google.com/viewer?url=http%3A%2F%2Felgg.wamped.org%2Ftest.ppt
But when I'm trying to serve the same file through a very simple php script to the viewer, it fails rendering with the not too helpful error message: "Sorry, we are unable to generate a view of the document at this time", see:
http://docs.google.com/viewer?url=http%3A%2F%2Felgg.wamped.org%2Freadfile.php
The script that serves the file is as follows:
<?php
header('Content-type: application/vnd.ms-powerpoint');
header('Content-Disposition: attachment; filename="test.ppt"');
readfile('test.ppt');
?>
I've tried playing around with various header fields like Pragma and Cache-Control, but nothing helped. I also tried slicing the output file and echoing in chunks, that also did not do any good. Checked the apache log on the server, checked the response headers, everything seems fine to me.
Any ideas what am I doing wrong here?
EDIT: Although I haven't found a solution to this issue, I stumbled upon a site that does the same (or seems to do more than that, actually) as Google Docs Viewer. http://embedit.in has support for a wide range of file types, has an API, and does the job nicely, so I'll just probably go with that one. However, out of curiousity, I'd still like to know what is wrong with the piece of code below. So any suggestions are more than welcome.
Have you tried naming the php file with a .ppt file extension instead of .php? Whether or not your server will process php code in a file that doesn't have a .php file extension is another problem. But Google Docs may simply say NO to loading any file with a .php extension on it.
Capitalize the T in Content-Type. I also provided the Content-Length header. My filehandler works now.
It looks like you might be missing some headers. You might also want to see if the file is in the same directory as readfile.php, because google might store each file on a remote server.

Download PHP script instead of executing it

I have a downloads directory on a website where I store a bunch of different files for people to download (zip, exe, java, php, etc). The problem is that my website is written in PHP, so the web server, Apache, tries to execute the scripts instead of letting people download them. Without having access to Apache config (I'm on shared hosting), what is the easiest way to prevent Apache from executing scripts in a single directory?
I tried using mod_mime unsuccessfully. AddType doesn't work because (I'm guessing) a MIME type is already associated with PHP scripts. ForceType doesn't work because I store different types of files in the directory. Are there any other options?
If you have sufficient permissions for that, putting the following line in a .htaccess file in the directory in which you don't want PHP script to be executed might do the trick :
php_flag engine off
(Just tested on my webserver, and I got the source of a PHP script -- which had not been executed)
You could write a separate PHP script which sends the right Content-type header and then uses readfile() to pass through the contents of the PHP file without PHP actually executing them (and since Apache already passed off the request to PHP, it no longer cares). Just make sure you restrict it to only serving things out of that directory.
I think the common solution to this is to give files the extension phps.
I think I have the solution for you. Check this out:
http://www.boutell.com/newfaq/creating/forcedownload.html
Basically, it says that you should have the following code in your php page:
<?php
header('Content-disposition: attachment; filename=whatever.php');
header('Content-type: text/html');
readfile('whatever.php');
?>
I made a sample here:
http://sotkra.com/test.php
This forces the 'download' file prompt where the file download is the whatever.php
Cheers
You should have a download gateway script, such as download.php. It should take a query string argument which lists the file that needs downloaded.
That argument should be matched against a pre-existing ARRAY of accessible files (big security point there).
Then use:
<?php
$file = trim(isset($_GET['file']) ? $_GET['file'] : '');
$allow = array(
'foo.php' => 'text/plain',
'foo.jpg' => 'image/jpeg',
);
if(! isset($allow[$file]))
die('File not found.');
header('Content-Type: ' . $allow[$file]);
readfile($file);

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