【问题标题】:PHP output file on disk to browser磁盘上的 PHP 输出文件到浏览器
【发布时间】:2020-10-30 20:42:49
【问题描述】:

我想用 PHP 向浏览器提供一个现有文件。 我看过有关 image/jpeg 的示例,但该功能似乎将文件保存到磁盘,您必须先创建一个大小合适的图像对象(或者我只是不明白它:))

在 asp.net 中,我通过读取字节数组中的文件然后调用 context.Response.BinaryWrite(bytearray) 来做到这一点,所以我在 PHP 中寻找类似的东西。

米歇尔

【问题讨论】:

    标签: php


    【解决方案1】:

    这应该可以帮助您开始: http://de.php.net/manual/en/function.readfile.php

    编辑:如果您的网络服务器支持它,使用

    header('X-Sendfile: ' . $filename);
    

    其中文件名包含本地路径,如

    /var/www/www.example.org/downloads/example.zip
    

    比 readfile() 快。

    (使用 header() 的通常安全注意事项适用)

    【讨论】:

      【解决方案2】:

      fpassthru() 应该可以满足您的需求。请参阅手册条目以了解以下示例:

      <?php
      
      // open the file in a binary mode
      $name = './img/ok.png';
      $fp = fopen($name, 'rb');
      
      // send the right headers
      header("Content-Type: image/png");
      header("Content-Length: " . filesize($name));
      
      // dump the picture and stop the script
      fpassthru($fp);
      exit;
      
      ?>
      

      请参阅here 了解 PHP 的所有文件系统函数。

      如果它是您要提供下载的二进制文件,您可能还想发送正确的标题,以便弹出“另存为..”对话框。请参阅this question 的第一个答案,了解发送哪些标头的一个很好的示例。

      【讨论】:

      • 如果我已经将二进制内容存储为变量怎么办。如何将其作为可下载文件输出到浏览器?
      【解决方案3】:

      对于我的网站和我为客户创建的网站,我使用了很久以前找到的 PHP 脚本。

      可以在这里找到:http://www.zubrag.com/scripts/download.php

      除了不允许热链接(默认)之外,我还使用了稍微修改过的版本,以允许我混淆文件系统结构(默认情况下),并且我添加了一些额外的跟踪功能,例如引荐来源网址、IP (默认),以及我可能需要的其他此类数据。

      希望这会有所帮助。

      【讨论】:

      • 感谢脚本。如果我想扩展我的第一个脚本,我肯定会使用它。
      【解决方案4】:

      我使用 readfile() (http://www.php.net/readfile)...

      但您必须确保使用 header() 设置正确的“Content-Type”,以便浏览器知道如何处理该文件。

      您也可以强制浏览器下载文件,而不是尝试使用插件来显示它(例如 PDF),我总是觉得这看起来有点“hacky”,但上面对此进行了解释链接。

      【讨论】:

        【解决方案5】:

        我用这个

          if (file_exists($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));
        
                ob_clean();
                flush();
                readfile($file);
                exit;
        
            } 
        

        【讨论】:

          【解决方案6】:

          以下将启动XML文件输出

          $fp = fopen($file_name, 'rb');
          
          // Set the header
          header("Content-Type: text/xml");
          header("Content-Length: " . filesize($file_name));
          header('Content-Disposition: attachment; filename="'.$file_name.'"');
          
          fpassthru($fp);
          exit;
          

          'Content-Disposition: attachment' 很常见,被 Facebook 等网站用来设置正确的标题

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多