【问题标题】:File download blocks everything文件下载会阻止一切
【发布时间】:2017-08-17 11:14:39
【问题描述】:

我正在下载一个包含以下代码的文件:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $new_filename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);


readfile($download_url);             
exit;

其中$download_url 是文件路径。文件正在完美下载,但我无法在第一次下载时下载同一页面上的下一个文件,甚至无法导航到其他页面,直到下载完成或取消。

【问题讨论】:

  • 那么想要达到什么目的呢?此外,我假设只要下载正在进行,页面就会加载,浏览器/客户端应该仍然响应并且不会出现“冻结”。
  • 否,下载时页面未加载。但是当我想下载第二个文件时,它开始处理。正如我所描述的,我无法导航到其他页面或下载其他文件。

标签: php download


【解决方案1】:

找到答案Here。我必须在输出文件之前关闭会话。

session_write_close();
readfile($download_url);

会话一次只能由一个 PHP 进程打开,任何其他发出 session_start() 命令的请求都会在等待访问会话数据文件时阻塞。

【讨论】:

    【解决方案2】:

    我在 Wamp 下试过,但有一些问题。

    经过大量搜索,我最终得到了这个工作代码

    $file = 'test.jpg';
    
    if (file_exists($file)) {
    
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
    

    【讨论】:

    • 不幸的是它对我不起作用。我尝试使用相同的标题,但文件名和大小是不同的变量。仍然没有运气。
    • 可能是浏览器的问题,可以私信导航试试吗?
    • 私人导航?
    • 那里也一样。
    • file_put_contents("test.jpg", fopen("http://someurl/test.jpg", 'r')); 呢?
    猜你喜欢
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多