首页 > 语言 > PHP > 正文

远程文件下载代码

2024-09-04 11:44:00
字体:大 中 小
来源:转载
供稿:网友

这里为各位提供一款远程文件下载代码,我们可以把远程的文件用php下载到本地指定的目录,下面就是一款下载远程服务器文件代码类。

  1. class download 
  2. { 
  3.     var $url;//远程文件地址 
  4.     var $file_name = "hdwiki.zip";//下载来的文件名称 
  5.     var $save_path = "./www.Vevb.com";//下载到本地的文件路径 
  6.     var $localfile;//下载到本地文件的路径和名称 
  7.     var $warning;//警告信息 
  8.     var $redown=0;//是否重新下载 
  9.  
  10.     /*初始化*/ 
  11.     function seturl($url) 
  12.     { 
  13.          if(!emptyempty($url))$this->url = $url; 
  14.     } 
  15.     function setfilename($file_name) 
  16.     { 
  17.      if(!emptyempty($file_name))$this->file_name = $file_name; 
  18.     } 
  19.     function setsavepath($save_path) 
  20.     { 
  21.      if(!emptyempty($save_path))$this->save_path = $save_path; 
  22.     } 
  23.     function setredown($redown) 
  24.     { 
  25.      if(!emptyempty($redown))$this->redown = $redown; 
  26.     } 
  27.     function download($url, $redown = 0, $save_path = 0, $file_name = 0) 
  28.     { 
  29.         $this->seturl($url); 
  30.         $this->setfilename($file_name); 
  31.         $this->setsavepath($save_path); 
  32.         $this->setredown($redown); 
  33.         if(!file_exists($this->save_path)) 
  34.         { 
  35.             $dir = explode("/",$this->save_path); 
  36.             foreach($dir as $p) 
  37.             mkdir($p); 
  38.         } 
  39.    } 
  40.     
  41.     /* 检查url合法性函数 */ 
  42.     function checkurl(){ 
  43.         return preg_match("/^(http|ftp)(://)([a-za-z0-9-_]+[./]+[w-_/]+.*)+$/i", $this->url); 
  44.     } 
  45.     //下载文件到本地 
  46.     function downloadfile() 
  47.     { 
  48.         //检测变量 
  49.         $this->localfile = $this->save_path."/".$this->file_name; 
  50.          if($this->url == "" || $this->localfile == ""){ 
  51.                  $this->warning = "error: 变量设置错误."; 
  52.              return $this->warning; 
  53.         } 
  54.         if (!$this->checkurl()){ 
  55.             $this->warning = "error: url ". $this->url ." 不合法."; 
  56.                return $this->warning; 
  57.             } 
  58.         if (file_exists($this->localfile)){ 
  59.             if($this->redown) 
  60.             { 
  61.                 unlink($this->localfile); 
  62.             } 
  63.             else 
  64.             { 
  65.                 $this->warning = "warning: 升级文件 ". $this->localfile ." 已经存在! 重新下载"; 
  66.                 return $this->warning; 
  67.              //exit("error: 本地文件 ". $this->localfile ." 已经存在,请删除或改名后重新运行本程序."); 
  68.             } 
  69.         } 
  70.         //打开远程文件 
  71.         $fp = fopen($this->url, "rb"); 
  72.         if (!$fp){ 
  73.             $this->warning = "error: 打开远程文件 ". $this->url ." 失败."; 
  74.              return $this->warning; 
  75.         } 
  76.      //打开本地文件 
  77.      $sp = fopen($this->localfile, "wb"); 
  78.      if (!$sp){ 
  79.          $this->warning = "error: 打开本地文件 ". $this->localfile ." 失败."; 
  80.          return $this->warning; 
  81.      } 
  82.      //下载远程文件 
  83.      //echo "正在下载远程文件,请等待"; 
  84.      while (!feof($fp)){ 
  85.      $tmpfile .= fread($fp, 1024); 
  86.      //echo strlen($tmpfile); 
  87.      } 
  88.        //保存文件到本地 
  89.        fwrite($sp, $tmpfile); 
  90.      fclose($fp); 
  91.      fclose($sp); 
  92.       
  93.      if($this->redown) 
  94.              $this->warning = "success: 重新下载文件 ". $this->file_name ." 成功"; 
  95.      else  
  96.              $this->warning = "success: 下载文件 ". $this->file_name ." 成功"; 
  97.                
  98.      return $this->warning; 
  99.     } 
  100. } 

上一篇:PHP分页类

下一篇:非常简单的日历类

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表