Jump to content

Is there such a thing as a URL_Exists function?


usafrmajor

Recommended Posts

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";
}	
?>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.