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.

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

PHP Image Watermarking

If you’ve ever wanted to watermark images on your website, this neat script will be just what you’re looking for.

Warez-DnB watermark

The Warez-DnB Watermark

The watermark image is held in a separate file, rather than having to actually edit each image with your watermark, save and upload, this script will serve the watermark to the user on the image as  and when its requested.

I like this approach to watermarking because no only is it much easier than editing each picture individually it also means with FTP access you still have an original image intact without the watermark. Only users connecting via HTTP will see the watermarked version.

This also allows for ultimate flexibility, if for whatever reason you decide to change the site logo, its as simple as replacing one image file and every image hosted will have the new watermark.

Read more…

Tags: ,
posted by Juo in tidbit and have Comments (2)