[英]PHP: check if image file exists using fopen
This question is about using fopen to check if a file exists, not cURL
or getimagesize
which are alternative methods but not what I am asking about. 这个问题是关于使用fopen检查文件是否存在,而不是cURL
或getimagesize
这是替代方法,但不是我要问的内容)。
I having been using following function in code for a couple years without problems and it is suddenly always returning FALSE
, even on valid images. 我在代码中使用了以下函数已经好几年了,但是突然间它总是返回FALSE
,即使是在有效图像上也是如此。 I don't know if I accidentally created a typo or if my host changed the version of PHP or what may have caused it but would appreciate it if anyone can spot what might be going wrong. 我不知道我是否偶然创建了一个错字,或者我的主机是否更改了PHP的版本,或者是什么原因引起的,但是如果有人能发现可能出了什么问题,我将不胜感激。
Here is code: 这是代码:
function image_exist($url) {
if (@fclose(@fopen( $url, "r "))) {
// true;
return TRUE;
} else {
// false;
return FALSE;
}
}
This is now returning FALSE
even on valid images. 现在即使在有效图像上也返回FALSE
。
Why use fopen()
and fclose()
when there's a function for this purpose? 有功能时,为什么要使用fopen()
和fclose()
?
function image_exist($url) {
return file_exists($url);
}
Edit: you're correct that this does not work for remote files over HTTP(S). 编辑:您是正确的,这不适用于HTTP(S)上的远程文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.