PHP checks whether the file exists under different domain names. InitiallyIusedthefile_existsfunction, butthenwhenthatthrewbackane PHP checks whether the file exists under different domain names.
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 und with varous functions, I came up with a few lines of code that actually works:
How to check if file exists on a different domain
? 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.
?
$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!!}
?
?