代码之家  ›  专栏  ›  技术社区  ›  Jon Winstanley

在PHP中强制下载图像。本地工作,但在Live Linux服务器上中断

  •  2
  • Jon Winstanley  · 技术社区  · 14 年前

    我强迫通过我的网站下载图片。

    强制下载在Apache/Windows开发机器上工作正常。

    然而,当它在我的LinuxWeb服务器上运行时,它会把垃圾字符推到屏幕上。

    e.g. �����JFIF��H�H����6Exif��MM�*����
    
    • 火狐-垃圾
    • 铬废料
    • Internet Explorer 7-在页面中显示图像

      $fileName = basename($filePath);
      $fileSize = filesize($filePath);
      
      
      // Output headers.
      header("Cache-Control: private");
      header("Content-Type: Image/jpeg");
      header("Content-Length: ".$fileSize);
      header("Content-Disposition: attachment; filename=".$fileName);
      
      
      // Output file.
      readfile ($filePath);                   
      exit();
      

    在我的实时服务器上可能会有什么不同导致它中断?

    3 回复  |  直到 14 年前
        1
  •  3
  •   Community CDub    7 年前

    stillstanding ,谁指出使用 fifo 但是我想在这里举个例子来帮助你。这个例子需要安装FIFO扩展,并且已经被黑客破解,并从我的其他代码中进行了轻微的修改。

        $filename = 'blarg.jpg';
        $filepath = '/foo/bar/blarg.jpg';
        $finfo    = new finfo(FILEINFO_MIME);
        $mime     = $finfo->file($file);
    
        // Provide a default type in case all else fails
        $mime = ($mime) ? $mime : 'application/octet-stream';
    
        header('Pragma: public');
        header('Content-Transfer-Encoding: binary');
        header('Content-type: ' . $mime);
        header('Content-Length: ' . filesize($filepath));
        header('Content-Disposition: attachment; filename="' . $filename . '"');
    
        header('Content-transfer-encoding: 8bit');
        header('Expires: 0');
        header('Pragma: cache');
        header('Cache-Control: private');
    
        2
  •  2
  •   bcosca    14 年前

    头中的mime类型不正确。使用 finfo 所以你可以发送正确的一个而不是作为 application/stream 否则,浏览器行为将是不可预测的。

        3
  •  1
  •   Jim    14 年前

    内容类型不应该设置为图像吗?