PHP- Check If File Exists On Different Domain
Earlier today I needed to find out if a file exists on a different domain. Initially I used the file_exists function, but then when that threw back an error I remembered that file_exists only checks whether a file or directory exists on the same server as the script.
After I played around with various functions, I came up with a few lines of code that actually works:
How to check if file exists on a different domain
1 2 3 4 5 6 7 8 9 10 11 12 | <? $image = "http://www.example.co.uk/images/1.jpg"; $handle = @fopen("$image", "r"); if(strpos($handle, "Resource id") !== false) { echo "file does exist"; } else { echo "file does not exist"; } ?> |
The logic explained
Ok, so if the file exists (1.jpg) the fopen function will throw back a “resource id” response. So I check the response to see if “response id” exists with the strpos function. It’s really as simple as that.
I’m not entirely sure if my method is the best, nor the most efficient, but it seems to work pretty well, and I can’t think of any other methods. Anyone know of any other/better methods?
Better solution
Thanks to a comment left by Paul I’ve been made aware of a better solution.
1 2 3 4 5 6 7 8 9 10 | $url = "http://www.example.com/index.php"; $header_response = get_headers($url, 1); if ( strpos( $header_response[0], "404" ) !== false ) { // FILE DOES NOT EXIST } else { // FILE EXISTS!! } |
brightcherry.co.uk
Nice. Cheers!
Couldn’t you just put the @fopen directly into the if expression? If it returns a resource it will be TRUE, if not it will be FALSE. Forget the strpos()
nice post. it was helpful.
It doesn’t work for me, it keep showing me this message fopen() [function.fopen]: failed to open stream: HTTP request failed!
file_get_contents(“http://example.com/path/to/image.gif”,0,null,0,1) is a much better solution!
You only need to examine the headers – don’t need the whole file such as in file_get_contents…
$arrHeader = get_headers($url, 1);
if ( strpos( $arrHeader[0], ’404′ ) !== false ){
//Got it!
} else {
Not so much!
}
Hey Paul,
Many thanks, that’s a great solution! I will update this post later
Learn something new every day…
@Steve file_get_contents on a remote file is unreliable; after a while it might give errors. Just check the internet on what I assume. Besides that, Paul is right; why read a whole file? Your solution is not good and surely not the best. Pauls is a far better one.
However, I’ve written a much larger function with fsockopen then Pauls, but the performance is also much better:
It took 0.003989 seconds to check http_get_headers, using Pauls function.
bool(true)=the result
It took 0.002345 seconds to check http_fsockopen, using my function.
bool(true)=the result
Just a tip.
Regards, Igor
Another example :
It took 0.338297 seconds to check http_get_headers [!]
It took 0.006532 seconds to check http_fsockopen on the same file.
I’ve done this test over and over again, as I want the best performance. There are examples on the internet, how to make this function; they are, however, never as good as mine; but mine is commercial, so I can’t give that away. Just keep in mind one tip : Never use preg_match!
Regards, Igor
http://curl.haxx.se/download.html
Place curl.exe in apache/bin/, enable php curl extension in php.ini and I think php must be registered as a apache module otherwise curl.exe must be placed in windows/system32/.
function rfile_exists($address)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$address);
curl_setopt($ch,CURLOPT_NOBODY,1);
curl_setopt($ch,CURLOPT_FAILONERROR,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
if(curl_exec($ch) !== FALSE)
{
return true;
}
return false;
curl_close($ch);
}
Thats a lot quicker way to do it.
Many Thanks,
Ian