首页 > 语言 > PHP > 正文

PHP远程下载类

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

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