Why So Scared

A blog about; Programming, Music and Random Stuff

PHP function check if a file (or url exists)

While doing some scripting for Warez-DnB (for a new feature that ill talk more about soon) I needed a script that would check if a file on the server existed.

The PHP code below does this job perfectly, returning a boolean TRUE if the file is found and FALSE if it isn’t .

function url_exists($url) {
// Version 4.x supported
$handle   = curl_init($url);
if (false === $handle)
{
return false;
}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
curl_setopt($handle, CURLOPT_HTTPHEADER, Array(”User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15″) ); // request as if Firefox
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$connectable = curl_exec($handle);
curl_close($handle);
return $connectable;
}

function url_exists($url) {

// Version 4.x supported

$handle   = curl_init($url);

if (false === $handle)

{

return false;

}

curl_setopt($handle, CURLOPT_HEADER, false);

curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works

curl_setopt($handle, CURLOPT_HTTPHEADER, Array(”User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15″) ); // request as if Firefox

curl_setopt($handle, CURLOPT_NOBODY, true);

curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);

$connectable = curl_exec($handle);

curl_close($handle);

return $connectable;

}

I thought someone else might find it useful.

posted by Juo in tidbit and have Comment (1)
Tags: ,

One Response to “PHP function check if a file (or url exists)”

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment