我有下一个脚本,它从服务器读取图像文件并将其返回给用户。浏览器(chrome和FF)无缘无故地停止显示图像,即使网络管理器中一切正常,标题和文件大小正确,并且在服务器上创建的文件本身也是如此。
if (!file_exists($previewFileName)) {
//... here comes creating the image code, I had tested it and the image is there!!!
}
//http://stackoverflow.com/a/15271869/2992810
session_write_close();
header("Content-Type: image/$ext");
//set cache header
$seconds_to_cache = -1; //three month
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: nocache");
header("Cache-Control: max-age=$seconds_to_cache");
//return the file
echo file_get_contents($previewFileName);
die();
这是回应 Heeaders:
HTTP/1.1 200 OK
Date: Sat, 22 Nov 2014 16:02:42 GMT
Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12
X-Powered-By: PHP/5.5.12
P3P: CP="CAO PSA OUR"
Expires: Sat, 22 Nov 2014 16:02:41 GMT
Cache-Control: max-age=-1
Pragma: nocache
Set-Cookie: kdm_csrf_cookie=d1bd278e07c72a69659d3c97471001b5; expires=Sat, 22-Nov-2014 18:02:42 GMT; Max-Age=7200; path=/
X-Frame-Options: SAMEORIGIN
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/jpg
Chrome 网络管理器输出 523KB 作为文件大小。但是,Chrome 或 FireFox 都没有显示图像本身。
我想也许图像本身坏了,但我直接从我的高清打开了它,它工作得很好,所以我不知道是什么导致浏览器不渲染图像。
有关如何进一步调试它的任何线索或想法将不胜感激......
适用于将来遇到图像相关错误的任何人。
在读取图像文件之前,请确认您没有将任何内容打印到屏幕上。
我不小心打印了 2 个空格,这导致图像读取失败!
该死!