Firefox : Open XLSX file not saving file butn opening binary - php

I generate a file server side and I want the client to automatically open it : it's a XLSX file. Firefox just opens the file and I see the binary content of the XLSX file in the browser, but I want it to be open via a Save As... box.
It works fine in Chrome with the same code (it saves it) but not firefox...
Any ideas ?

Have a look at this - Php exec and return binary
Are you sending proper headers??
something like
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfile.xlsx\"");
UPDATE
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=example.xlsx');
header('Pragma: no-cache');
echo file_get_contents("/path/to/yourfile.xlsx");
UPDATE 2
Spread sheet mime types
application/vnd.ms-excel [official]
application/msexcel
application/x-msexcel
application/x-ms-excel
application/vnd.ms-excel
application/x-excel
application/x-dos_ms_excel
application/xls
UPDATE 3
Regarding your javascript problem did you try using
location.href instead of window.open ??

You need to ensure you are sending this mime type as the Content-Type header:-
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
So you need to map the .xslx extension to this mime type on the server

Related

Rename a file with PHP right before it downloads without saving it to the server?

I have an Adobe Illustrator file (AI) that we currently have a link to on a website which then downloads the file to your computer.
The link looks something like this...
http://domain.com/crm/index.php?entryPoint=fileupload_download&id=22440435-e8ee-bd6f-7612-533b2cd7690f&field=fuaifile_c&type=D1_Designs
What I need to do is rename this file as it downloads.
So I am asking if it is possible to pass this download through another PHP file right before it downloads which would allow me to change the filename on the fly that the user downloads. I cannot change the filename on the server but when it downloads I would like to be able to add some ID numbers to the filename on the fly if this is possibble? Any ideas how to accomplish this without having to resave the image on the server with a new name?
What you are looking for is the Content-Disposition header, as specified in RFC 2183:
Content-Disposition: attachment; filename=example.ai
You can set this header using the PHP header() function.
It's ugly, and assumes these aren't "large" files that would exceed your memory_limit, but
$data = file_get_contents($original_url);
header('Content-disposition: attachment; filename="new name with id numbers');
header("Content-type: application/octet-stream");
echo $data;
You could always enhance this to do byte serving - suck 10k from original url, spit out 10k to user, etc...
Just set the Content-Disposition:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
(Example taken from PHP docs: http://www.php.net/manual/en/function.header.php).
Adding id:
$id = generateIdFromSomewhere();
header('Content-Disposition: attachment; filename="downloaded'.$id.'.pdf"');

Utility of HTTP header "Content-Type: application/force-download" for mobile?

I am currently working on a PHP script that allows you to download media contents (video, audio, pictures...) from your mobile device by accessing a link. (i.e. http://www.my-web-site.com/download.php?id=7ejs8ap)
My script worked very vell when I was testing it with recent mobile (Samsung Galaxy S, iPhone 4S, some others...) but an error occured on my old mobile Samsung C3050. The media I wanted to download was just an audio mp3 file that I usually download easily.
The error appears to be "Unknown content type."
So, as my only HTTP header Content-Type was "application/force-download", I try to comment this and try again. Then, it works.
But now, I am currently asking what this Content-Type means and if it can be mandatory for others mobile. I tested without the Content-Type on the iPhone 4 and it works, but I'm not sure of this compatibility for all mobile.
Can someone explain me how that Content-Type works, why this isn't a standard MIME or everything else that can help me to be sure this is an optionnal Content-Type for every download, whatever the file, the browser or the device I am downloading on?
Thanks everyone.
Here is my PHP headers sent:
<?php
//Assume that $filename and $filePath are correclty set.
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
// header('Content-Type: application/force-download'); Non-standard MIME-Type, incompatible with Samsung C3050 for example. Let it commented
readfile($filePath);
?>
EDIT : I just tried with a Sony Xperia, and the download wasn't successful: I only see the "html-encoded" bytes of my file I want to download.
How can I know what content-type I have to use if application/octet-stream or application/force-download doesn't work?
Content-Type: application/force-download means "I, the web server, am going to lie to you (the browser) about what this file is so that you will not treat it as a PDF/Word Document/MP3/whatever and prompt the user to save the mysterious file to disk instead". It is a dirty hack that breaks horribly when the client doesn't do "save to disk".
Use the correct mime type for whatever media you are using (e.g. audio/mpeg for mp3).
Use the Content-Disposition: attachment; etc etc header if you want to encourage the client to download it instead of following the default behaviour.
To download a file please use the following code ... Store the File name with location in $file variable. It supports all mime type
$file = "location of file to download"
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
To know about Mime types please refer to this link: http://php.net/manual/en/function.mime-content-type.php
application/force-download is not a standard MIME type. It's a hack supported by some browsers, added fairly recently.
Your question doesn't really make any sense. It's like asking why Internet Explorer 4 doesn't support the latest CSS 3 functionality.

php force download xml

I am creating an xml file on the fly. When a user generates this file I want it to open up a download file dialog with the content that was generated. There is no actual file because it is just generated through php. Any ideas on how to do this?
This is what worked for me. In readfile('newfile.xml'); make sure to give the path of the file correctly. This php page is called from an html page with anchor tag which says - download:
<?php
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('newfile.xml');
?>
source: How do I force the browser to download a file to disk instead of playing or displaying it?
Send a content-disposition attachment header.
header('Content-disposition: attachment; filename=advertise.xml');
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
// if you want to directly download then set expires time
header("Expires: 0");

Not being prompted to download CSV file

I've created a custom solution in WordPress that will generate a CSV file to be downloaded by clicking a simple hyperlink, linked directly to this file. Instead of being prompted to download the file to the computer; the CSV opens in the the browser window instead.
FWIW I'm on Media Temple using a vanilla install of WordPress.
Send the proper mime type
header('Content-type: text/csv');
And use the Content-Disposition header to tell it to download: http://www.jtricks.com/bits/content_disposition.html
header('Content-Disposition: attachment; filename="mycssfile.csv"');
You always want to send the proper mime type, otherwise firewalls, anti-virus software and some browsers may have issues with it...
You can use PHP's header() function to change Content-type
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="myFile.csv"');
The above code will force a prompt to the user for download. where myFile.csv should be replaced with the path to the file you want downloaded.
This works:
$filename = 'export.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
Also, I personally do not like links on my sites, I like buttons. If you want a button to do for the export function you can use the code below. I just thought I would post it because it took me a bit to figure out the first time :)
<input type="button" value="Export to CSV" onClick="window.location.href='something.php?action=your_action';"/>
You need to send the browser a MIME type of application/csv so it will offload the responsibility of handling the file to whatever the OS recommends (or user chooses).
In PHP (before any output is sent to the client):
header('Content-type: application/csv');

How to use the CSV MIME-type?

In a web application I am working on, the user can click on a link to a CSV file. There is no header set for the mime-type, so the browser just renders it as text. I would like for this file to be sent as a .csv file, so the user can directly open it with calc, excel, gnumeric, etc.
header('Content-Type: text/csv');
echo "cell 1, cell 2";
This code works as expected on my computer (Isn't that how it always is?) but does not work on another computer.
My browser is a nightly build of FF 3.0.1 (on linux). The browsers it did not work in were IE 7 and FF 3.0 (on windows)
Are there any quirks I am unaware of?
You could try to force the browser to open a "Save As..." dialog by doing something like:
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
echo "cell 1, cell 2";
Which should work across most major browsers.
You are not specifying a language or framework, but the following header is used for file downloads:
"Content-Disposition: attachment; filename=abc.csv"
With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly..
header('Pragma: public');
Just my 2 cents..
This code can be used to export any file, including csv
// application/octet-stream tells the browser not to try to interpret the file
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($data));
header('Content-Disposition: attachment; filename="export.csv"');
I tried to use text/csv but it did not work for me after that tried different stuff and I figured out that if we use this text/plain. Now the file upload is completed. as expected. This problem was in the Yii2 file upload widget.
Example response after success.
{"files":[{"name":"unit_ids list - Sheet1.csv","type":"text/plain","size":30,"base_url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co","path":"1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co/1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","delete_url":"/coupons/default/sheet-delete?path=1%2Fg2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv"}]}

Categories