启用URL重写时,PHP file_exists()无法检查文件是否存在?
问题描述:
in Zend FW:
file_exists('xx/xxx/xxxx.jpg');
returned 1, but xx/xxx/xxxx.jpg is not file or directory.
it's an error page returned by Zend FW. because it's not a file or valid url
how can I check file existence?
: p>
file_exists('xx / xxx /xxxx.jpg');
返回1,但xx / xxx / xxxx.jpg不是文件或目录。 p>
这是Zend FW返回的错误页面。 因为它不是文件或有效网址 p>
如何检查文件是否存在? p> div>
答
To check for existence over HTTP, you need to check the HTTP response codes. The easiest way to do that IMO is using curl. Try this:
$url = 'http://google.com/this_does_not_exist.jpg';
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);//timeout
$res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch ($code % 100){
case 2: echo "exists
"; break;
case 3: echo "too many redirects
"; break;
case 4: echo "does not exist
"; break;
case 5: echo "5xx error - may or may not exist once server fixed
"; break;
default:
echo "Something weird. HTTP response code = $code
";
}