天天看點

php從url下載下傳,從URL将檔案下載下傳到伺服器

我用這個下載下傳檔案

function cURLcheckBasicFunctions()

{

if( !function_exists("curl_init") &&

!function_exists("curl_setopt") &&

!function_exists("curl_exec") &&

!function_exists("curl_close") ) return false;

else return true;

}

function cURLdownload($url, $file)

{

if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";

$ch = curl_init();

if($ch)

{

$fp = fopen($file, "w");

if($fp)

{

if( !curl_setopt($ch, CURLOPT_URL, $url) )

{

fclose($fp); // to match fopen()

curl_close($ch); // to match curl_init()

return "FAIL: curl_setopt(CURLOPT_URL)";

}

if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {

curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');

if( !curl_setopt($ch, CURLOPT_HEADER, $curlopt_header)) return "FAIL: curl_setopt(CURLOPT_HEADER)";

if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";

if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";

if( !curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects) ) return "FAIL: curl_setopt(CURLOPT_MAXREDIRS)";

return curl_exec($ch);

} else {

curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');

if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";

if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";

if( !curl_setopt($ch, CURLOPT_HEADER, true)) return "FAIL: curl_setopt(CURLOPT_HEADER)";

if( !curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) return "FAIL: curl_setopt(CURLOPT_RETURNTRANSFER)";

if( !curl_setopt($ch, CURLOPT_FORBID_REUSE, false)) return "FAIL: curl_setopt(CURLOPT_FORBID_REUSE)";

curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');

}

// if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";

// if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";

// if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";

if( !curl_exec($ch) ) return "FAIL: curl_exec()";

curl_close($ch);

fclose($fp);

return "SUCCESS: $file [$url]";

}

else return "FAIL: fopen()";

}

else return "FAIL: curl_init()";

}