2

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

LookAtMeNow
  • 261
  • 1
  • 5
  • 15
  • Probably because of the spaces. I don't think you can change the way a browser interprets a filename (not entirely true as FF is open-source). Use `str_replace` on `basename` to change the spaces to e.g. underscores. – AmazingDreams Aug 08 '13 at 10:00
  • 1
    you didn't close the quote in the content-disposition header. try this: header('Content-Disposition: attachment; filename=' . basename($file_url)."'"); – Mircea Soaica Aug 08 '13 at 10:00

3 Answers3

2
header('Content-Disposition: attachment; filename="' .basename($file_url).'"');
Amit Kumar Sharma
  • 262
  • 1
  • 4
  • 12
1

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.

Dexa
  • 1,641
  • 10
  • 25
1

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);

JTC
  • 3,344
  • 3
  • 28
  • 46