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 21getimagesize($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?
@is generally frowned upon, but I believe this is one of its few legitimate use cases. As long as you anticipate and handle the potential error condition, suppressing the error message is acceptable (IMHO). Having said that, you shouldn't worry about the error message in production anyway becausedisplay_errorsshould be off ;-)