I know that there are at least 10 the same questions with answers but none of them seems to work for me flawlessly. I'm trying to check if internal or external image exists (is image URL valid?).
fopen($url, 'r')
fails unless I use@fopen()
:Warning: fopen(http://example.com/img.jpg) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in file.php on line 21
getimagesize($img)
fails when image doesn't exist (PHP 5.3.8):Warning: getimagesize() [function.getimagesize]: php_network_getaddresses: getaddrinfo failed
- CURL fails because it isn't supported by some servers (although it's present mostly everywhere).
fileExists()
fails because it doesn't work with external URLs and can't possibly check if we're dealing with image.
Four methods that are the most common answers to such question are wrong. What would be the correct way to do that?
This code is actually to check file... But, it does works for images!
I know you wrote "without curl" but still, somebody may find this helpfull:
You will see someting like this
HTTP/1.1 200 OK
orHTTP/1.1 404 Not Found
in returned header array. You can do also multiple parallel requests withcurl multi
.getimagesize($img) fails when image doesn't exist:
am not sure you understand what you want .....FROM PHP DOC
Example
Output
getimagesize
works just fineEdit