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 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

    点赞 评论 复制链接分享

相关推荐