龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

远程下载文件

时间:2014-07-22 14:50来源: 作者: 点击:
分享到:
strongspan style=font-family:Courier New;font-size:16px;HttpDownload/span/strong span style=font-family:Courier New;font-size:16px;::/span span style=color:#003399;font-family:Courier New;font-size:16px;save2dir/span span style=font-family:C
HttpDownload :: save2dir ($url$ localname$ forcibly ); 
 
$forcibly (逼迫模式)
如果有重名文件 则自动改名 [  待续  ]
<?php

final class HttpDownload {
	
	public static $filesize;
	public static $Downloadstatus = FALSE;
	
	private static function getfilesize($url) {
		$headers = get_headers($url, 1);
		if($headers[0] == 'HTTP/1.1 200 OK') {
			if(isset($headers['Content-Length'])) {
				return self::$filesize = (int)$headers['Content-Length'];
			}
		}
		return self::$filesize = (int)0;
	}
	
	public static function save2dir($url, $localname, $forcibly = TRUE) {
		$filesize = self::getfilesize($url);
		if(is_resource(@fopen($url, 'r'))) {
			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('http://www.baidu.com/index.html', 'baidu.html');
if(HttpDownload::$Downloadstatus) {
	echo 'success';
}else{
	echo 'failed';
}
精彩图集

赞助商链接