【问题标题】:Force file download with php using header()使用 header() 强制使用 php 下载文件
【发布时间】:2012-01-19 03:07:06
【问题描述】:

我希望用户能够下载我服务器上的一些文件,但是当我尝试使用互联网上的许多此类示例中的任何一个时,似乎对我没有任何帮助。我试过这样的代码:

<?php

$size = filesize("Image.png");

header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
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: ' . $size);
readfile("Image.png");

我什至尝试使用我能找到的最基本的例子,像这样:

<?php
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
readfile('Image.png');

当我对此进行测试时,我已经删除了我拥有的所有其他代码,并使用一个仅包含此代码的空文件来删除由外部源创建的任何错误。

当我查看控制台时,文件会以正确的标题发送,即

'Content-Disposition: attachment; filename="Image.png"'

但没有显示保存对话框。

我也尝试在内容处置标题中使用内联而不是附件,但这也没有什么不同,我已经在 Firefox 8.0.1 Chrome 15.0.874.121 和 Safari 5.1.1 中对此进行了测试。

【问题讨论】:

    标签: php http-headers


    【解决方案1】:

    我很确定您不会在文件下载中将 mime 类型添加为 JPEG:

    header('Content-Type: image/png');
    

    这些标题从来没有让我失望过:

    $quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
    $size   = filesize($file);
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $quoted); 
    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);
    

    【讨论】:

    • 当我尝试你的标题时,我得到了和以前一样的结果,但是我在查看萤火虫控制台时注意到一些东西,由于某种原因,尾随的 " 不存在,我不知道那是不是一个问题,但我尝试删除它们,但它也没有帮助。我用于测试的文件名中没有空格,所以我不知道是否需要它?
    【解决方案2】:

    这对我来说就像下载 PNG 和 PDF 的魅力一样。

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$file_name.'"');
    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_url)); //Absolute URL
    ob_clean();
    flush();
    readfile($file_url); //Absolute URL
    exit();
    

    【讨论】:

      【解决方案3】:

      基于Farhan Sahibole's answer:

              header('Content-Disposition: attachment; filename=Image.png');
              header('Content-Type: application/octet-stream'); // Downloading on Android might fail without this
              ob_clean();
      
              readfile($file);
      

      这就是我工作所需要的一切。我剥离了所有不需要的东西。

      关键是使用ob_clean();

      【讨论】:

        【解决方案4】:

        问题是我使用 ajax 将消息发布到服务器,当我使用直接链接下载文件时一切正常。

        我改用了其他 Stackoverflow 问答材料,它对我很有用:

        【讨论】:

        • 所以,您的意思是您实际上并没有删除所有其他代码,如您的问题所述。
        【解决方案5】:

        它对我有用

        $attachment_location = "filePath";
        if (file_exists($attachment_location)) {
        
            header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
            header("Cache-Control: public"); // needed for internet explorer
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length:".filesize($attachment_location));
            header("Content-Disposition: attachment; filename=filePath");
            readfile($attachment_location);
            die();
        } else {
            die("Error: File not found.");
        }
        

        【讨论】:

          【解决方案6】:

          htaccess 解决方案

          <filesmatch "\.(?i:doc|odf|pdf|cer|txt)$">
            Header set Content-Disposition attachment
          </FilesMatch>
          

          您可以阅读此页面: https://www.techmesto.com/force-files-to-download-using-htaccess/

          【讨论】:

            【解决方案7】:

            这是我在测试中的一个 sn-p... 显然通过 get 传递给脚本可能不是最好的...应该发布或只是发送一个 id 并从 db 获取 guid...无论如何.. 这个工作。我获取 URL 并将其转换为路径。

            // Initialize a file URL to the variable
            $file = $_GET['url'];
            $file = str_replace(Polepluginforrvms_Plugin::$install_url, $DOC_ROOT.'/pole-permitter/', $file );
            
            $quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
            $size   = filesize($file);
            
            header( "Content-type: application/octet-stream" );
            header( "Content-Disposition: attachment; filename={$quoted}" );
            header( "Content-length: " . $size );
            header( "Pragma: no-cache" );
            header( "Expires: 0" );
            readfile( "{$file}" );
            

            【讨论】:

              猜你喜欢
              • 2010-11-03
              • 2010-11-30
              • 1970-01-01
              • 2011-11-08
              • 2012-11-11
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多