>백엔드 개발 >PHP 튜토리얼 >php file_get_contents 無法顯示圖片

php file_get_contents 無法顯示圖片

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-06 20:42:01912검색

<code><?php $url = "http://nsv.ckcdn.com/images/logo1.png";
$refer = "http://nsv.ckcdn.com/";
$opt=array("http"=>array("header"=>"Referer: " . $refer)); 
$context=stream_context_create($opt); 
$file_contents = file_get_contents($url,false, $context);
echo $file_contents;
?>
</code>

我想偽造Referer,然後用 file_get_contents 取得圖片顯示出來,不過顯示出來的是亂碼,該如何解決?
php file_get_contents 無法顯示圖片

回复内容:

<code><?php $url = "http://nsv.ckcdn.com/images/logo1.png";
$refer = "http://nsv.ckcdn.com/";
$opt=array("http"=>array("header"=>"Referer: " . $refer)); 
$context=stream_context_create($opt); 
$file_contents = file_get_contents($url,false, $context);
echo $file_contents;
?>
</code>

我想偽造Referer,然後用 file_get_contents 取得圖片顯示出來,不過顯示出來的是亂碼,該如何解決?
php file_get_contents 無法顯示圖片

<code><?php header("Content-type: image/png");
$url = 'http://misc.360buyimg.com/lib/img/e/logo-201305.png';
$file_contents = file_get_contents($url);
echo $file_contents;
?>
</code>

结果可以正常输出 某东 的LOGO(因为题主提供的图片链接访问不到,所以找了同为PNG格式的LOGO)。

我觉得问题出在file_get_contents上。文档:http://cn2.php.net/file_get_contents
因为file_get_contents的作用是整个文件读入一个字符串中,输出是当然是以字符串输出,浏览器不知道它是图片,所以要在加上header("Content-type: image/png"),以告诉浏览器这是png格式的图片,浏览器才会显示成图片。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.