Dublicate error happened in chrome, opera, safari and only works on firefox.
Here's the header:
| Header
| Forcing a download using readfile()
|----------------
*/
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_mime_type);
header('Content-Disposition: attachment; filename=' . $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: ' . $file_size);
ob_clean();
flush();
readfile($file_path);
exit;
I've tried to put double quotes and works but adds to file name - for e.g. -'filename.pdf'
header("Content-Disposition: attachment; filename= . '$file'");
To fix issue you need
// Change this
header('Content-Disposition: attachment; filename=' . $file);
// To this
header('Content-Disposition: attachment; filename="'.$file.'"');
Related
I have a database from which I would like the user to download data
I'm trying to download mp3 files. If the file contains, for example, Cyrillic characters, then in Safari I get the file ÐÐ¸Ð·Ð½ÐµÑ lite - ÐÑÐ°Ñ talk.mp3, in other browsers I get a normal file name Бизнес lite - Краш talk.mp3. Here is a sample code. Help, please, what am I doing wrong?
`
$src_file = ROOT_PATH . '/files/uploads/' . substr( $track_file['name'], 0, 2) . '/' . $track_file['name'];
if(file_exists($src_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. $track_file['title'] . '.mp3"');
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($src_file));
ob_clean();
flush();
readfile($src_file);
exit;
`
would use if else
if user agent safari . you can find browser name by the $_SERVER['HTTP_USER_AGENT']
$src_file = ROOT_PATH . '/files/uploads/' . substr( $track_file['name'], 0, 2) . '/' . $track_file['name'];
if(file_exists($src_file)) {
if($useragent=='safari'){
header('content-type: application/octet-stream');
header('content-Disposition: attachment; filename='.$src_file);
header('Pragma: no-cache');
header('Expires: 0');
readfile($src_file);
}else{
//your current code
}
I see this tutorial to make download file but I have a problem
Here is my example
$file_url = "D:/my file name.doc"
header('Content-Type: text/json; charset=UTF-8;');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file_url)."");
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_url));
ob_clean();
flush();
readfile($file_url);
Everything's well in ie or chrome. But when I using firefox to download file. The file download has my is the file name? How to fix that thanks
header('Content-Disposition: attachment; filename="' .basename($file_url).'"');
First of all you are missing ; on the end of the first line.
I think the problem is in filename with spaces.
Try to use readfile(urlencode($file_url)); instead of readfile($file_url);
You should quote filename.
header('Content-Disposition: attachment; filename="' . basename($file_url) . '"');
EDIT: It was as selected answer just with typo in it. Now I fixed it.
I need make xlsx file download from my site (but not from directly open file url like this: http://site.com/file.xlsx )
So, this is php code
$file = "somefile.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
file is downloaded, his extension is .xlsx, but when trying open this file in ms excel, file not opened and I got error : excel cannot open the file.xlsx because the file format or file extension is not valid
Tell please, why this happened? where I am wrong?
After many years, I got same problem, and after searching, I got here again ))
This is solution, that worked for me:
$file = "somefile.xlsx";
// define file $mime type here
ob_end_clean(); // this is solution
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($file);
You must be using this code in middle of some other file.
The problem with headers is they need to be set first on a page. They will not work if you have even 1 single space echoing before them. So you need to ob_clean() [clean the buffer] before you are setting headers
Try
ob_clean();
flush();
$file = "somefile.xlsx";
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
Remove:
ob_clean();
flush();
Add at the end of code:
exit();
The issue is that flush() will also throw in your *.xlsx file content some garbage it has in it and that will corupt your file, even if you use ob_clean();
For a better understanding go to php.net and read the difference between flush(), ob_flush() and find that you didn't even need them in the first case. Therefore you won't need the ob_clean() too.
This works for me:
header('Content-Description: File Transfer');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".basename($fileLocation)."\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Length: ' . filesize($fileLocation)); //Remove
ob_clean();
flush();
readfile($fileLocation);
I have 2 files - 1.csv and 2.csv. I can download 1.csv with PHP by known approach:
$file = '1.csv';
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
But my purpose is to have in downloaded file content of both files - 1.csv and 2.csv. Does anybody have some ideas how to implement this?
Thank you in advance.
If I understand you correctly this should do it.
$file1 = '1.csv';
$file2 = '2.csv';
header('Content-Description: File Transfer');
header('Content-Type: text/comma-separated-values');
header('Content-Disposition: attachment; filename='.basename($file1, '.csv') . '-' . basename($file2, '.csv') . '.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . (filesize($file1) + filesize($file2)));
ob_clean();
flush();
readfile($file1);
readfile($file2);
Consider using a loop for multiple files and mind the change of the Content-Type header for CSV files.
$file1 = '1.csv';
$file2 = '2.csv';
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename="1-2.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . (filesize($file1)+filesize($file2)));
ob_clean();
flush();
echo file_get_contents($file1).file_get_contents($file2);
Downloads the concatenation of the 2 file contents
I'm forcing a download on links using this code from the PHP.net site:
if (file_exists($file)) {
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);
exit;
}
I put this code in a file called 'download.php', and call it from a page (Click).
It works fine on all browsers, but on Safari the window it opens ('download.php') stays open during and after the download/open process. In other browsers it disappears immediately. Has anyone else come across this problem, and if so are there any suggestions as to how to solve it?
I tried this and it seems to be working equally in all browsers. Try this code, considering that Safari and IE need of more characters as headers to be sent:
#$file = 'load.php';
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();
for($i = 0; $i < 40000; $i++) {
echo ' '; // extra spaces
}
flush();
usleep(50000);
exit;