dounuogong0358 2010-11-25 10:04
浏览 74
已采纳

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

I am forcing the download of an image through my website.

Forced download works fine on Apache/Windows development machine.

However it pushes junk characters to the screen when live on my linux web server.

e.g. �����JFIF��H�H����6Exif��MM�*����
  • Firefox - junk
  • Chrome - junk
  • Internet Explorer 7 - displays the image in the page

    $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();
    

What differences might there be on my live server that would cause it to break?

  • 写回答

3条回答 默认 最新

  • doru52911 2010-11-25 12:38
    关注

    Hat tip (and +1) to stillstanding, who pointed out using fifo, but I thought I'd provide an example here to help. This example requires the fifo extension installed, and has been hacked out and slightly modified from some other code of mine.

        $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条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题