读取文件无法正常工作
我正在尝试通过流式传输方式通过服务器将文件下载给我。
这是我的脚本:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=http://user:[email protected]/file.bin';
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: ' . $r[2]);
ob_clean();
flush();
readfile($fileName);
exit;
example.com 不是我真正使用的,我只是不想显示真实的 URL。 :) 当我在网络浏览器中访问此文件时,不会出现下载提示,也没有任何反应。 $r[2]
是文件的内容长度(以字节为单位),$fileName
是附件标头中使用的相同 URL。我真的不知道我做错了什么。
I'm trying to download a file through my server to me, by streaming the download.
This is my script:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=http://user:[email protected]/file.bin';
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: ' . $r[2]);
ob_clean();
flush();
readfile($fileName);
exit;
example.com isn't what I'm really using, I just didn't want to show the real URL. :) When I go to this file in my web browser a download prompt does not come up and nothing happens. $r[2]
is the content length in bytes of the file and $fileName
is the same URL as used in the attachment header. I really don't know what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
读取文件();需要文件在机器上的绝对路径而不是 url。
例如/home/person/file.exe
readfile(); needs the absolute path of the file on the machine rather than a url.
e.g. /home/person/file.exe
文件名标题行末尾缺少
)
,但我假设编辑该行时发生了这种情况。对于它的价值,运行示例代码会给出下载提示,过去还没有真正对其进行测试,但也许它与服务器/客户端配置有关 - 而不是代码。
There's a missing
)
at the end of the filename header line, but I'm assuming that happened when edited the line.For what it's worth, running your sample code on gives a download prompt, haven't really tested it much past that, but maybe it's something with the server/client configuration - not the code.
恕我直言,问题是文件大小,尝试通过http获取文件大小(看看这个函数):
http://codesnippets.joyent.com/posts/show/1214
因此编辑从
到
希望你能解决你的问题。
IMHO the problem is the filesize, try to get the filesize over http (look at this function):
http://codesnippets.joyent.com/posts/show/1214
So edit from
to
Hope you solve your issue.
例如,我找到了一种下载图像的方法,但这对任何类型的文件都有帮助。它下载文件时不使用
readfile
。我知道这可能很笨拙,但有时我们需要一些解决方法来完成工作。
试试这个,其中
$src = 'http://external-server/remote-image.jpg'
这已经过测试并且可以工作;-)
I found a way to download an image for example, but this could help with any kind of file. It downloads a file without using
readfile
.I know it can be clumsy but sometimes we need some workarounds to get the job done.
Try this, where
$src = 'http://external-server/remote-image.jpg'
this is tested and working ;-)