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

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式