dongli4711 2015-11-08 20:36
浏览 16
已采纳

从php脚本开始下载MP4

So I have this small PHP condition for a PHP download, it's been working fine for weeks then all of a sudden I noticed it's stopped working.

Rather than getting a download of the file, the users are getting a 0kb file instead.

I have checked the URL and it's still working as expected.

Here is the code

if ($refer == 'stackoverflow.com' || $refer == 'dev.stackoverflow.com') {

        $fichero = $downloadUrl;

        if ($fichero) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($fichero));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($fichero));
                ob_clean();
                flush();
                readfile($fichero);
                exit;
        } else {
                die("The File $fichero does not exist");
        }
} else {
        die("Sorry you must start the download from the website");
}

Now I know it's hitting the conditionals, because I am not getting any of the die statements and the file is still being offered as a download. This seems to have broken in Chrome and also IE(Edge). I have been doing some reading regarding headers and the only documentation on this proble I can find that appears to be relevant is the expires setting which i set to 0.

Am I missing an easy trick here?

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • douhan4093 2015-11-08 21:03
    关注
    header('Content-Length: ' . filesize($fichero));
    

    Was causing the issue, I should have debugged this a little more before posting.

    This file needed write in order to suppress this warning

    PHP Warning: filesize(): stat failed for

    When removing

    header('Content-Length: ' . filesize($fichero));
    

    The code worked as expected, I tweaked with chmod on the file and re-enabled the above code and it fixed my issue.

    Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?