file_get_contents显示图像不正确 [英] file_get_contents displaying image incorrectly

查看:58
本文介绍了file_get_contents显示图像不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将通用网站和URL显示为我自己的网站的一部分.这是我正在使用的相对简单的代码的一部分:

browse.php

<?   
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>

如果URls是相对的而不是绝对的,则可以忽略某些链接/图像不起作用的事实,这很好.页面是使用$ _GET访问的,类似

browse.php?url = http://itracki.com

该网页将按预期显示.但是,当我尝试获取其他东西(例如图像)时,会得到类似的东西,我认为它是二进制文件还是其他东西?

browse.php?url = http://images.itracki.com /2011/06/favicon.png

‰PNG  IHDRóÿa%IDAT8Ëc8sæÌJ0M ```ã3`xaÔ€aa]r#f.–ÄíNIEND®B`‚

我一直在网上寻找并尝试修改页面标题,例如

<? header('Content-type: image/png;'); ?>

但是我仍然得到同样的混乱结果.如果有人通过键入" http://images.itracki.com/2011/06/favicon.png "添加到地址栏中?

谢谢!

解决方案

尝试一下:

<?php
  $img = 'http://images.itracki.com/2011/06/favicon.png';
  $info = getimagesize($img);
  header('Content-type: ' . $info['mime']);
  readfile($img);
?>

在这种情况下,应使用readfile()而不是file_get_contents(),因为readfile()会将文件内容直接发送到浏览器,而不是将其存储在内存中供以后检索,这是file_get_contents()的作用. /p>

还请注意,content-type是动态检索的,因此您不必担心在标头中错误地指定它.

某些图像的扩展名可能与content-typemime-type不匹配,因此可以纠正这些类型的问题.

I'm trying to display generic websites and URLs as part of my own site. Here's part of the relatively simple code I'm using:

browse.php

<?   
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>

Ignoring the fact that some links/images will not work if the URls are relative rather than absolute, this works just fine. Pages are accessed using $_GET, something like

browse.php?url=http://itracki.com

The webpage will display as expected. However, when I'm trying to get something else, like an image, I get something like this, which I'm thinking is binary or something?

browse.php?url=http://images.itracki.com/2011/06/favicon.png

‰PNG  IHDRóÿa%IDAT8Ëc8sæÌJ0M ```ã3`xaÔ€aa]r#f.–ÄíNIEND®B`‚

I've been looking online and tried modifying the page headers, something like

<? header('Content-type: image/png;'); ?>

but I'm still getting the same jumbled up result. Does anyone know how I can actually see the image as it is if I were just accessing it by typing "http://images.itracki.com/2011/06/favicon.png" into the address bar?

Thanks!

解决方案

Try this:

<?php
  $img = 'http://images.itracki.com/2011/06/favicon.png';
  $info = getimagesize($img);
  header('Content-type: ' . $info['mime']);
  readfile($img);
?>

You should use readfile() instead of file_get_contents() in this situation because readfile() will send the file contents directly to the browser rather than storing it in memory for a later retrieval, which is what file_get_contents() does.

Also note that the content-type is retrieved dynamically so that you don't have to worry about specifying it incorrectly in the header.

Some images may have an extension that doesn't match its content-type or mime-type, so this corrects for those type of issues.

这篇关于file_get_contents显示图像不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆