读取文件无法正常工作

发布于 2024-08-14 06:07:54 字数 798 浏览 6 评论 0原文

我正在尝试通过流式传输方式通过服务器将文件下载给我。

这是我的脚本:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

π浅易 2024-08-21 06:07:54

读取文件();需要文件在机器上的绝对路径而不是 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

千秋岁 2024-08-21 06:07:54

文件名标题行末尾缺少 ) ,但我假设编辑该行时发生了这种情况。

对于它的价值,运行示例代码会给出下载提示,过去还没有真正对其进行测试,但也许它与服务器/客户端配置有关 - 而不是代码。

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.

她说她爱他 2024-08-21 06:07:54

恕我直言,问题是文件大小,尝试通过http获取文件大小(看看这个函数):
http://codesnippets.joyent.com/posts/show/1214

因此编辑从

header('Content-Length: ' . $r[2]);

header('Content-Length: ' . get_remote_file_size($fileurl) );

希望你能解决你的问题。

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

header('Content-Length: ' . $r[2]);

to

header('Content-Length: ' . get_remote_file_size($fileurl) );

Hope you solve your issue.

蓝眸 2024-08-21 06:07:54

例如,我找到了一种下载图像的方法,但这对任何类型的文件都有帮助。它下载文件时不使用readfile

我知道这可能很笨拙,但有时我们需要一些解决方法来完成工作。

试试这个,其中 $src = 'http://external-server/remote-image.jpg'

$url_stuff = parse_url($src); 
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80; 
$fp = fsockopen($url_stuff['host'], $port); 
$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n"; 
$query .= 'Host: ' . $url_stuff['host']; 
$query .= "\n\n"; 
$buffer=null;
fwrite($fp, $query); 
while ($tmp = fread($fp, 1024)) 
{ 
    $buffer .= $tmp; 
} 

preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts); 
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=your_file.bin');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$content = substr($buffer, - $parts[1]); 
echo $content;

这已经过测试并且可以工作;-)

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'

$url_stuff = parse_url($src); 
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80; 
$fp = fsockopen($url_stuff['host'], $port); 
$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n"; 
$query .= 'Host: ' . $url_stuff['host']; 
$query .= "\n\n"; 
$buffer=null;
fwrite($fp, $query); 
while ($tmp = fread($fp, 1024)) 
{ 
    $buffer .= $tmp; 
} 

preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts); 
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=your_file.bin');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$content = substr($buffer, - $parts[1]); 
echo $content;

this is tested and working ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文