Is there a function to check if a file exists?

Tell me if there is such a function that it would process a link like “http://anysite.ru/image.jpg” and return ‘true’ if this image exists and ‘false’ if it doesn’t?


Answer 1, authority 100%

file_exists

file_exists('  '); //    true  false   

and don’t forget that the path is written server-side and not http, i.e. something like $_SERVER['DOCUMENT_ROOT'] . '/img/someimg.jpg'instead of http://siteName/img/someimg.jpg

When it comes to files on other sites
As an option, try to get the file_get_contentsimage, put it in a variable and check for “emptiness …”

In the case of pictures, it’s probably better to use getimagesize(); it doesn’t really matter why it is actually needed, in general, here is a code example with getimagesize():

$img = getimagesize('http://somesite/someimg');
if($img) {
     //  :)
}
else {
    // :(          
    //  ...
}

A small explanation of why – if, for example, a non-standard 404 is made on the site, then in file_get_contentswe will legally have info, but there is no image 🙂 The probability of this is small, but if the programmer for the “non-standard 404 page ” set the header 404 – then everything will be fine, but personally I think it’s better to overdo it than not do it ^^…

By the way, the result of running this function in the case of an image existence is an array with information about the image: height, width and format.

and an example with file_get_contents

$img = file_get_contents('http://somesite/someimg');
if($img) {
    //        :)
}
else {
    // ...
}

Answer 2, authority 25%

file_get_contents(‘http: etc’);