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
Related
I have a PHP script which is downloading file after it is created.
Everything is working correctly when using PC but when I want to load same script on my iPad for example I can't download it. I'm getting error that file is broken.
What could be the problem?
My headers in PHP file:
header('Content-Description: File Transfer');
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=offer.doc");
I have a PHP script which is downloading file after it is created.
My bet is you're using HTML to create the file, and giving it a .doc extension to make it look like a Word document.
While this is totally okay and supported by all versions of Word AFAIK, a 3rd party viewer program like on an iPad may be more strict. A HTML based file is technically not a Word document, and the viewer may not be equipped to parse it accordingly.
You may need to resort to generating a real .doc file.
I have form for file uploading in my website that i check mime-type of uploaded file to allow or refuse uploading it. It worked well until I've upgraded my firefox to 3.6.11 (in ubuntu). But now uploading Doc files is not possible. I checked the mime-type of file but it was 'application/x-php' instead of 'application/vnd.ms-word' or other msword mime-types.
I use
echo $_FILES[$fileName]['type'];
to see the mime-type. I upload the same file from firefox 8 and firefox 3.6 and the output of above code was:
FireFox 8: application/vnd.ms-word
FireFox 3.6: application/x-php
Is it a bug of firefox or I have to change my codes?
I use PHP.
$_FILES[...]['type'] is just arbitrary, user supplied, best-guess, unreliable (as you see) information supplied by the client which may or may not have anything to do with the actual file. Never use it.
Try to detect the MIME type yourself on the server. For example techniques, see How to get the content-type of a file in 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
I'm seeing some strange behavior from some dynamically generated PDFs using the TCPDF library in PHP.
Standard lamp stack-- however, if you try to open the PDF from Windows with Acrobat Reader, it gives an error that the "file is damaged and could not be repaired". From Mac, Linux, etc. the file works fine, and opens fine. It also opens fine in Google Docs-- so clearly the PDF itself is ok.
Is it possible that the mime type (application/pdf) is causing problems in Windows?
Thanks
What browser on Windows? All? Or just one? My initial gut instict, is that the Windows browser is ignoring the encoding IF the content is being gzipped (Ignoring the Content-Encoding header). That's if you're even sending that header.
Open the file you downloaded in some text editor (Notepad, etc). The first few characters of the file should be %PDF-1. with another number after it. If it's not at the beginning, check to see if the file is gzipped (rename the file to blah.gz, and then run it through gzip to try to decode it). If that worked, then your problem is the browser ignoring the encoding.
IF it is ignoring the encoding, you need to not gzip the output of that PHP file. How you do that will depend on your server configuration.
Oh, and the application/pdf is the proper mime type. And the mime type is not your problem, since Acrobat is at least trying to open the file...
I am storing files into an image field in SQL server storing the string in hex after converting into using:
unpack("H*hex", $datastring);
If I read from the database and write to a file, then open the file locally I can open it up just fine. But if I try to send the file as an attachment to the browser the file becomes corrupted and unreadable. Currently, the files in question are PDF and MSWord documents.
I am setting content-type to the appropriate MIME type for the files and setting content-disposition to attachment. I've tried various ways of streaming the file including dumping the string directly from the database and writing first to a file then reading the file (either line by line or with readfile()).
I am using a slightly customized version of the Zend framework, but I'm not sure if that is causing any issues. What should I do to send files to the browser?
How do you serve them?, in theory if you are using MVC, you need to disable the view at the end of your controller to avoid extra content being inyected at the bottom of your file.