PHP图像内容类型问题 [英] PHP Image content type problem
问题描述
对于我最近的项目,我需要一个简单的PHP脚本,它根据通过其ID发送的图像显示图像URL。以下是代码:
header(Content-type:image / jpeg);
$ img = $ _GET [img];
echo file_get_contents(http://www.somesite.hr/images/$img);
问题在于图片不显示,虽然浏览器可以识别它(我可以看到它它可以打印出图像的URL。
它既不能在支持远程访问的服务器上运行,也不能在没有服务器的情况下运行。
此外,在标题前没有任何内容被打印或回显。
我想知道它是内容类型错误还是别的。
在此先感谢。
可能图像不适合记忆。或者您的PHP安装无权进行外部HTTP调用。无论如何,我建议你不要使用echo file_get_contents(),而是使用 readfile 。
您也不应该使用$ _GET或$ _POST的原始字符串进行文件操作。总是去除用户提供的文件名中的空字节,斜线和双点,或者更好的是,只允许字母数字字符。
I have a specific problem, and cant get over it.
For my latest project I need a simple PHP script that display an image according to its ID sent through URL. Here's the code:
header("Content-type: image/jpeg");
$img = $_GET["img"];
echo file_get_contents("http://www.somesite.hr/images/$img");
The problem is that the image doesn't show although the browser recognizes it (i can see it in the page title), instead I get the image URL printed out.
It doesn't work neither on a server with remote access allowed nor with one without. Also, nothing is printed or echoed before the header.
I wonder if it is a content type error, or something else. Thanks in advance.
Possibly the image doesn't fit into memory. Or your PHP installation doesn't have permissions to make external HTTP calls. Anyway, I suggest you never use echo file_get_contents(), use readfile instead. Also you should never use raw strings from $_GET or $_POST for file operations. Always strip null-bytes, slashes and double dots from user-provided filenames, or better yet, allow only alphanumeric characters.
这篇关于PHP图像内容类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!