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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.