php file_get_contents 無法顯示圖片

WBOY
Release: 2016-06-06 20:42:01
Original
835 people have browsed it

<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>
Copy after login
Copy after login

我想偽造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>
Copy after login
Copy after login

我想偽造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>
Copy after login

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

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!