简体   繁体   English

受保护下载的HTTP标头

[英]HTTP Headers on Protected Downloads

So I'm using this article to generate and protect some download links on my website 因此,我正在使用本文来生成和保护我网站上的一些下载链接

The problem I'm having is the script works, just not with huge files. 我遇到的问题是脚本可以正常工作,但不能用于大型文件。 For example, a 1GB file downloads perfectly, whilst a 7GB one doesn't. 例如,一个1GB的文件可以完美下载,而一个7GB的文件则不能完美下载。

Someone in the comments mentioned large downloads might cause this. 评论中提到某人下载量较大可能导致此情况。

My headers looks like this at the moment: 目前,我的标题如下:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($fakefilename) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($realfilename));
ob_clean();
flush();
readfile($realfilename);
exit;

And this guide is suggesting they should look more like this: 指南建议他们应该看起来像这样:

header("Content-Disposition: attachment; filename=" . urlencode($file));    
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");             
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.

$fp = fopen($file, "r"); 
while (!feof($fp))
{
    echo fread($fp, 65536); 
    flush(); // this is essential for large downloads
}  
fclose($fp); 

For the life of me, I can't get this to work. 为了我的一生,我无法解决这个问题。 Anyone here that is familar with HTTP headers and while large but not small files can't be downloaded? 这里有人熟悉HTTP标头,却不能下载大而不小文件吗?

Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance! 提前致谢!

This error is caused by the filesize call overflowing the systems PHP_MAX_INT which yields a length less then that of the entire file. 该错误是由于filesize调用溢出系统PHP_MAX_INT导致的,其长度小于整个文件的长度。 (Which is about 2GB on a 32bit machine) (在32位计算机上约为2GB)

Only browsers that trust the content-length header over the size of the file itself appear to be afflicted. 只有信任内容长度标头超过文件本身大小的浏览器才会出现问题。

Solution is to change 解决方案是改变

header("Content-Length: " . filesize($file));

to

header("Content-Length: " . exec("stat -c %s ".escapeshellarg($filename)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM