首页 >php教程 >PHP源码 >远程下载文件

远程下载文件

PHP中文网
PHP中文网原创
2016-05-25 17:09:191023浏览

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