代码之家  ›  专栏  ›  技术社区  ›  unholysampler

强制下载停止工作

  •  1
  • unholysampler  · 技术社区  · 14 年前

    我有一个PHP脚本,多年来我一直使用它来强制从我的网站下载。但在上个月左右的某个时候,它停止了工作,并触发了“找不到文件”错误。奇怪的是,在火狐中,如果我在错误页面上查看源代码,那就是我试图下载的文件。然后执行“文件”>“从那里保存”将为您提供正确的文件。所以我知道脚本在服务器上找不到文件不是问题。

    我设置邮件头的方式有问题吗?

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); 
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: Binary');
    header('Content-length: '.filesize($file_url));
    header('Content-disposition: attachment; filename="'.basename($file_url).'"');
    readfile($file_url);
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Shubham    14 年前

    你能试试这个功能吗?

    function force_download($file){
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        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: ' . filesize($file));
        readfile($file);
        exit;   
    }
    
        2
  •  0
  •   unholysampler    14 年前

    我最终靠作弊来达到目的。

    header("Location: $file_url"); //file_url is now the real url, not the path
    

    然后使用cpanel确保我使用的所有mime类型都设置为 application/octet-stream .