This is what I did. It covers more possible results with getting headers, because if you cannot access the file, it is not always just “404 Not Found”. Sometimes these are "Moved Permanentently", "Forbidden" and other possible messages. However, it is simply “200 OK” if the file exists and can be accessed. The HTTP part may have 1.1 or 1.0 after it, so I just used strpos to be more reliable for every situation.
$file_headers = @get_headers( 'http://example.com/image.jpg' );
$is_the_file_accessable = true;
if( strpos( $file_headers[0], ' 200 OK' ) !== false ){
$is_the_file_accessable = false;
}
if( $is_the_file_accessable ){
}
else
{
}
source
share