首页 > php file_get_contents 無法顯示圖片

php file_get_contents 無法顯示圖片

<?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;
?>

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


<?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;
?>

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

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

【热门文章】
【热门文章】