【问题标题】:Broken image with readfile and file_get_contents使用 readfile 和 file_get_contents 损坏的图像
【发布时间】:2011-08-18 03:23:32
【问题描述】:

我正在尝试使用一个函数来使其正常工作:

<img src='login.php?image=ind_legend.jpg'>

但是我不能通过函数来​​放置图像。我倒退了几步,只尝试了这部分代码:

<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
?>

或使用此功能:

echo file_get_contents($source);

但事实是,我得到的唯一结果是图像交叉损坏 (IE) 或 Firefox 中什么都没有。

如果有任何建议,我将不胜感激

提前致谢

【问题讨论】:

    标签: php image file-get-contents readfile


    【解决方案1】:

    您的 PHP 脚本中肯定有一些空格,或者 UTF-8 BOM 在您的 &lt;?php 开头标记之前不可见。使用十六进制编辑器找出答案。

    要进一步调试,请直接在浏览器中打开图像 URL http://localhost/login.php?image=ind_legend.jpg,保存文件。然后将其与原始 JPEG 进行比较。

    【讨论】:

      【解决方案2】:

      首先将您的浏览器指向http://youraddress/login.php?image=ind_legend.jpg 并检查结果。

      可能文件 /sales/test.jpg 已损坏,或者您没有为 readline 启用 http:// 包装器

      最后通过保存图像为...保存损坏的图像...您选择的浏览器的上下文菜单选项,并尝试使用文本编辑器打开它。如果您发现错误消息(如果您启用了它们),我不会感到惊讶。

      【讨论】:

        【解决方案3】:

        如前所述,您可能有一些空格。我会尝试用下面的代码替换整个文件。删除结束的 php 语句消除了任何有多余空格的机会:

        <?php
        $file = "http://localhost/sales/test.jpg";
        header('Content-type: image/jpeg');
        readfile($file);
        

        【讨论】:

          【解决方案4】:

          readfile()

          之前使用php的ob_clean()函数

          【讨论】:

          • 这是一种解决问题的方法,问题的实际解决方案在接受的答案中。
          猜你喜欢
          • 2013-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-21
          相关资源
          最近更新 更多