Heim  >  Artikel  >  php教程  >  远程下载文件

远程下载文件

PHP中文网
PHP中文网Original
2016-05-25 17:09:19955Durchsuche

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;;
}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn