标题不正确?

时间:2014-08-02 17:33:33

标签: php header download push

我想做的事情:

我试图将jpg文件推送到用户看到URL下载。在这种情况下,文件位于:http://www.example.com/upload/asdasdsadpokdaspdso/36.jpg

我目前的代码:

header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$download->name.'.jpg"');
readfile($weburl."/upload/".$hiddenpassage."/".$download->link);

我的vars / db值:

 $weburl = "http://wwww.example.com";
 $hiddenpassage = "asdasdsadpokdaspdso";

 $download->link = 36.jpg //not a var, just drom db.
 $download->name = The First Test Product //not a var, just from db.

问题:

当我下载时,我打开它,我收到以下错误:

The file “The First Test Product (28).jpg” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognize.

将.jpg重命名为.txt:

http://pastebin.com/K9NGL5RP

大部分内容都是我从中下载的页面内容。

1 个答案:

答案 0 :(得分:0)

我认为你需要指定整个标题(未经测试)。特别是Content-Length。:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$download->name.'.jpg');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($weburl."/upload/".$hiddenpassage."/".$download->link));
readfile($weburl."/upload/".$hiddenpassage."/".$download->link);

希望这有帮助。