Home  >  Article  >  php教程  >  远程下载文件

远程下载文件

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn