Php 如何在所有浏览器中强制下载文件?

Php 如何在所有浏览器中强制下载文件?,php,Php,我正在编写一个脚本,该脚本允许用户使用php下载pptx和zip文件,但该脚本在不同浏览器上的行为不同。我在互联网上尝试了很多脚本,但都没有正常工作,所以我制作了这一个,从不同的脚本中收集块 firefox=>工作完美 Opera=>将文件作为HTM文件下载 Safari=>0kb文件 IE=>捕获旧文件 我的代码:- // content type for pptx file $ctype = "application/vnd.openxmlformats-officedocument.pr

我正在编写一个脚本,该脚本允许用户使用php下载pptx和zip文件,但该脚本在不同浏览器上的行为不同。我在互联网上尝试了很多脚本,但都没有正常工作,所以我制作了这一个,从不同的脚本中收集块

  • firefox=>工作完美
  • Opera=>将文件作为HTM文件下载
  • Safari=>0kb文件
  • IE=>捕获旧文件
  • 我的代码:-

    // content type for pptx file 
    $ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    $file = "http://www.abc.com/presentation.pptx";
    header("Pragma: public"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: ".$ctype); 
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".filesize($file)); 
    
    ob_clean(); 
    flush(); 
    readfile( $file);
    
    如何让所有浏览器可靠地下载文件,而不是显示上面看似随机的行为

    编辑:下面是为我工作的代码,我不确定是什么问题,不需要的标题或文件路径?我做了这两个改变,它成功了

    $ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    $file = "Presentation3.pptx";
    header("Pragma: public"); 
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-Type: ".$ctype); 
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".filesize($file)); 
    readfile( $file); 
    

    在我对你的代码的测试中,如果使用本地文件而不是url和url,那么也给我0 KB

    我在firefox上测试,IE,Chrome所有结果都是一样的

      // content type for pptx file 
        $ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    
        $file = "presentation.pptx";//attention  
    
        header("Pragma: public"); 
        header("Expires: 0"); 
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Cache-Control: private",false); // required for certain browsers 
        header("Content-Type: ".$ctype); 
        header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); 
        header("Content-Transfer-Encoding: binary"); 
        header("Content-Length: ".filesize($file)); 
    
        ob_clean(); 
        flush(); 
        readfile( $file); 
    

    如果文件在您的服务器中,请不要使用URL。若它是一个外部文件,我会先将其写入您的服务器,然后将其打印给用户

    我还将删除
    标题(“缓存控制:private”,false)行;并使用
    标题('Expires:Mon,Jul 26 1997 05:00:00 GMT')而不是
    标题(“过期:0”)。对我来说总是这样:)


    同时尝试删除
    ob_clean();冲洗()(因为在发送文件之前没有要刷新或清理的内容)

    请注意,IOS浏览器在下载时存在一些问题

    你的问题是什么?看我的答案,我认为这对你很有帮助@vascowhite:问题是:文件下载在不同的浏览器上工作不同这不是问题,这是陈述。您是在寻找原因的解释,还是在寻找代码来解决您遇到的一些问题?@vascowhite-:)好的,我想知道代码中缺少了什么。我遵循了您的建议和Mohammads的建议,并且在所有浏览器中都能正常工作。ThanksI遵循了您的建议和Marcelo的建议,在所有浏览器中都可以正常工作。ThanksI在Windows7、Safari 5.1.7上进行了测试,顺便问一下,你在ios浏览器中遇到了什么问题??ThanksI在safari中试用过ipod、ipad等,在mac windows等平台上也能正常工作。我尝试添加音频下载功能。但不是下载,而是开始流媒体。你能帮我个忙吗?请尝试下载pptx和zip?