Wednesday, April 08, 2009

How to access a file on a remote server?

There are 2 simple way to do that. First, using fopen(), second, cURL functions.

Below is a the sample code for servers that support cURL:
------------ --------- --------- --------- --------- -----
//////////// ///////// ///////// ///////// ////////
function url_exists($ strURL) {
$resURL = curl_init();
curl_setopt( $resURL, CURLOPT_URL, $strURL);
// return the result instead of echoing it
curl_setopt( $resURL, CURLOPT_RETURNTRANS FER, 1);
// time out after 5 seconds of no response
curl_setopt( $resURL, CURLOPT_TIMEOUT, 5);

$result = curl_exec ($resURL);
if ($result === false) {
curl_close($ resURL);
return false;
}

$intReturnCode = curl_getinfo( $resURL, CURLINFO_HTTP_ CODE);
curl_close ($resURL);

if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode !=
304) {
return false;
}

return true;
}
//////////// ///////// ///////// ///////// ////////

No comments: