usafrmajor Posted August 5, 2009 Share Posted August 5, 2009 I would like to see if a file exists on a different web domain hosted by a different company and was wondering if there was a function like file_exists() that does this when the file is located on the same web domain server. thanks Alan Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/ Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2009 Share Posted August 5, 2009 I would use the HTTP status header to see if the file is valid. If a 200 response is returned then the file is definatly there. Use fsockopen() to do this. Example: <?php // obtain http header for a given url filepath function file_exists_from_http_header($server, $filepath) { $fp = fsockopen($server,80,$errno,$errstr,30); $out = "GET /".$filepath." HTTP/1.1\r\n"; $out .= "Host: $server\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp,$out); $content = fgets($fp); if(strstr($content, "200 OK")) { // file exists return true; } return false; } // usage if(file_exists_from_http_header("www.google.co.uk", "intl/en_uk/images/logo.gif")) { print "file exists"; } else { print "file does not exist"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-891424 Share on other sites More sharing options...
usafrmajor Posted August 6, 2009 Author Share Posted August 6, 2009 Can't seem to get this to work. I just cut and pasted your code for the function and have verified that the domain name is correct and the file does exist in the directory I enter as the second parameter. Any thoughts on what I could be doing wrong? thanks Alan Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892117 Share on other sites More sharing options...
trq Posted August 6, 2009 Share Posted August 6, 2009 Not in html theres not. Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892121 Share on other sites More sharing options...
JonnoTheDev Posted August 6, 2009 Share Posted August 6, 2009 Can't seem to get this to work. I just cut and pasted your code for the function and have verified that the domain name is correct and the file does exist in the directory I enter as the second parameter. Have you saved as a php file i.e test.php http://www.yourdomain.com/test.php and yes, you should have posted this in PHP help not HTML Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892126 Share on other sites More sharing options...
usafrmajor Posted August 6, 2009 Author Share Posted August 6, 2009 What other kind of file would I save it to? And, considering this site is for all kinds of help associated with PHP I was looking for the forum that I thought most closely applied to my problem. thanks Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892365 Share on other sites More sharing options...
Mardoxx Posted August 6, 2009 Share Posted August 6, 2009 yes there is do what neil.johnson but using javascript Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892446 Share on other sites More sharing options...
JonnoTheDev Posted August 6, 2009 Share Posted August 6, 2009 what is the error Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892526 Share on other sites More sharing options...
haku Posted August 6, 2009 Share Posted August 6, 2009 Did you try it with google as was in the original code? Quote Link to comment https://forums.phpfreaks.com/topic/168941-is-there-such-a-thing-as-a-url_exists-function/#findComment-892579 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.