Download KML File Instead of View in Browser - php

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

Related

PHP display PDF from internal file server

I have an internal server running PHP and an internal file directory with some work instructions pdfs for our technicians to read from. I'm trying to add a link to those pdf files so the technicians can click on the link and open the pdf in the browser.
So I have echo "' . "Work Instruction Link"
and I get Security Error: Content at http://LocalServer:50563/workInstructions.php may not load or link to file:///path/to/workInstruction.pdf
I understand the security risk of not being able to access the local files of a user from a web page, but I don't understand why I can paste the same file url into the address bar and the file will display on the web page. How are those two mechanisms different?
Is there a way to make that link work while serving it from my PHP server, or am I just going about the problem completely wrong? Is there something wrong with my formatting or syntax that I'm not catching?
I noticed that the error response does not contain the server in the file name. I don't exactly know what that means, or why that is the case.
My question is not answered by this post: html-File URL "Not allowed to load local resource in the internet browser because 1. it is asking within classic ASP which is a very different technology from PHP and 2. While it does have the same error message it does not explain how to have a link to files hosted on the same server as the webserver.
I have also tried the answer to this question: Point a link to a certain server
but it did not work for me when i try to convert "FILE://fileserver/path/to/workInstruction.pdf" to "Http://fileserver/path/to/workInstruction.pdf" I'm guessing because it's a file server and not a webserver that the pdf is located on.
To further pinpoint my question, is there a way in PHP to serve a link to a file hosted on a local server that the user can click and open a PDF in the browser?
For anyone else coming to this looking for an answer I have found this answer worked for me:
PHP-Display PDF on browser
Essentially I take the file path of the PDF I'm trying to load that I get from my fileserver and redirect to a new php file that reads the file and displays it in a new tab
The php file that contains the link will have something like this:
echo ''.$wi_num.'';
My workInstruction.php looks like this:
<?php
$filename= $_GET['filename'];
$filePath= $_GET['filepath'];
header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
#readfile($filePath);
?>

Getting Safari to Write Out Correct FileName

I'm building a web app with PHP that uses Excel Writer (XML) for PHP to create an Excel file that the user can download. I've taken a look at the source code for the library, and all it does is echo the generated XML to standard output. Although the file being generated is an XML file, I specifically give it a name with a ".xls" extension so that after it's been downloaded the end-user can double-click it and it will launch Excel on their system and open the file successfully.
This works correctly when downloading the file with Firefox, Chrome and IE, but not with Safari. For example, if I set the name of the file to be 'File.xls', this is the name of the file that gets downloaded. But with Safari the name of the file ends up being 'File.xls.xml'.
The server is running Apache on Mac OS X. I thought that might matter as I'm guessing that the problem has to do with MIME types on the server, but I'm not at all sure. Perhaps I can do something with the link that appears on the page, or perhaps I need to edit a configuration file for Apache? Any help is appreciated.
You might be right, it could just be the MIME type that needs setting.
Try putting
header("Content-Type: application/vnd.ms-excel");
before the output code, that should force the browser to see the data as an Excel file.
If the code is setting Content-Type already and it's conflicting, then search the script for "Content-Type" and try changing it.
Also, if it's an XLSX file, then there's a different MIME type - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

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.

What HTTP headers do I need to stream an ASF file?

I have a simple PHP script that will either serve up a streaming ASF file or not, depending on whether you're logged in and have access to the file. It basically does this:
<?php
header('Content-Type: video/x-ms-asf');
header('Content-Disposition: inline; filename="file.asf"');
readfile('file.asf');
?>
This already works fine in Firefox; when you navigate to this file it starts the video streaming right away. But Internet Explorer is a different story. When I go to this link in IE, it consistently tries to download the file as if it were an attachment rather than streaming it in the browser. What I am missing that IE's getting hung up on?
I don't think you need to add the Content-Disposition. Did you try removing it?
It look like that your IE or Media Player isn't correctly configured. Check this link link text for more details.
From your description, it sounds like your IE is configured to save a .asf file rather than stream it. Perhaps you can try opening a similar file on your local drive in IE to check whether it's configured to stream it.

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