首頁  >  文章  >  php教程  >  远程下载文件

远程下载文件

PHP中文网
PHP中文网原創
2016-05-25 17:09:19993瀏覽

php代码

<?php

final class HttpDownload {
	
	public static $filesize;
	public static $Downloadstatus = FALSE;
	
	private static function getfilesize($url) {
		$headers = get_headers($url, 1);
		if($headers[0] == &#39;HTTP/1.1 200 OK&#39;) {
			if(isset($headers[&#39;Content-Length&#39;])) {
				return self::$filesize = (int)$headers[&#39;Content-Length&#39;];
			}
		}
		return self::$filesize = (int)0;
	}
	
	public static function save2dir($url, $localname, $forcibly = TRUE) {
		$filesize = self::getfilesize($url);
		if(is_resource(@fopen($url, &#39;r&#39;))) {
			set_time_limit(0);
			if($forcibly) {
				if(file_exists($localname)) unlink($localname);
			}
			copy($url, $localname);
			return self::$Downloadstatus = (bool)true;
		}
		return self::$Downloadstatus = (bool)false;
	}
	
	
}

HttpDownload::save2dir(&#39;http://www.baidu.com/index.html&#39;, &#39;baidu.html&#39;);
if(HttpDownload::$Downloadstatus) {
	echo &#39;success&#39;;
}else{
	echo &#39;failed&#39;;
}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn