"Referer: " . $refer)); $context=stream_context_create($opt); $file_contents = file_get_contents($url,fa">
当前位置:Gxlcms > PHP教程 > phpfile_get_contents無法顯示圖片

phpfile_get_contents無法顯示圖片

时间:2021-07-01 10:21:17 帮助过:3人阅读

array("header"=>"Referer: " . $refer)); 
$context=stream_context_create($opt); 
$file_contents = file_get_contents($url,false, $context);
echo $file_contents;
?>

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

回复内容:

array("header"=>"Referer: " . $refer)); 
$context=stream_context_create($opt); 
$file_contents = file_get_contents($url,false, $context);
echo $file_contents;
?>

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


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

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

人气教程排行