it.vsesobe.ru

Web-технологии.

Скачивание файла со строннего ресурса через curl

Функция для скачивания файла со стороннего ресурса через curl файл сохраняется в папку tmp , которую нужно разместить рядом с файлом скрипта
$path="http://orel.dev.ranepa.ru/upload/iblock/1c1/KudriavtsevVA.pdf";
//$path="http://vsesobe.ru/img/34/34_8.jpg";

function dlFile($url) {
	set_time_limit(0);
	$name = str_replace('%20',' ',end(explode('/',parse_url($url,PHP_URL_PATH))));
	$path = dirname(__FILE__) . '/tmp/'.$name;
	$fp = fopen ($path, 'w+');
	try{
	//Here is the file we are downloading, replace spaces with %20
	$ch = curl_init(str_replace(" ","%20",$url));
	curl_setopt($ch, CURLOPT_TIMEOUT, 90);
	// write curl response to file
	curl_setopt($ch, CURLOPT_FILE, $fp); 
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
	curl_setopt($ch, CURLOPT_USERAGENT, ($_SERVER['HTTP_USER_AGENT'] ?: 'Mozilla/5.0 (Linux; CentOS)').' LK.RANEPA');
	// razumov-aa
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
	
	// get curl response
	curl_exec($ch); 
	curl_close($ch);
	}catch(Exception $err) {
		var_dump($err);
	}
	fclose($fp);
	return $path;
}



var_dump(dlFile($path));