这里为各位提供一款远程文件下载代码,我们可以把远程的文件用php下载到本地指定的目录,下面就是一款下载远程服务器文件代码类。
- class download
- {
- var $url;
- var $file_name = "hdwiki.zip";
- var $save_path = "./www.phpfensi.com";
- var $localfile;
- var $warning;
- var $redown=0;
-
-
- function seturl($url)
- {
- if(!emptyempty($url))$this->url = $url;
- }
- function setfilename($file_name)
- {
- if(!emptyempty($file_name))$this->file_name = $file_name;
- }
- function setsavepath($save_path)
- {
- if(!emptyempty($save_path))$this->save_path = $save_path;
- }
- function setredown($redown)
- {
- if(!emptyempty($redown))$this->redown = $redown;
- }
- function download($url, $redown = 0, $save_path = 0, $file_name = 0)
- {
- $this->seturl($url);
- $this->setfilename($file_name);
- $this->setsavepath($save_path);
- $this->setredown($redown);
- if(!file_exists($this->save_path))
- {
- $dir = explode("/",$this->save_path);
- foreach($dir as $p)
- mkdir($p);
- }
- }
-
-
- function checkurl(){
- return preg_match("/^(http|ftp)(://)([a-za-z0-9-_]+[./]+[w-_/]+.*)+$/i", $this->url);
- }
-
- function downloadfile()
- {
-
- $this->localfile = $this->save_path."/".$this->file_name;
- if($this->url == "" || $this->localfile == ""){
- $this->warning = "error: 变量设置错误.";
- return $this->warning;
- }
- if (!$this->checkurl()){
- $this->warning = "error: url ". $this->url ." 不合法.";
- return $this->warning;
- }
- if (file_exists($this->localfile)){
- if($this->redown)
- {
- unlink($this->localfile);
- }
- else
- {
- $this->warning = "warning: 升级文件 ". $this->localfile ." 已经存在! 重新下载";
- return $this->warning;
-
- }
- }
-
- $fp = fopen($this->url, "rb");
- if (!$fp){
- $this->warning = "error: 打开远程文件 ". $this->url ." 失败.";
- return $this->warning;
- }
-
- $sp = fopen($this->localfile, "wb");
- if (!$sp){
- $this->warning = "error: 打开本地文件 ". $this->localfile ." 失败.";
- return $this->warning;
- }
-
-
- while (!feof($fp)){
- $tmpfile .= fread($fp, 1024);
-
- }
-
- fwrite($sp, $tmpfile);
- fclose($fp);
- fclose($sp);
-
- if($this->redown)
- $this->warning = "success: 重新下载文件 ". $this->file_name ." 成功";
- else
- $this->warning = "success: 下载文件 ". $this->file_name ." 成功";
-
- return $this->warning;
- }
- }
|