当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP远程下载类

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

Tags: PHP远程下载类

分享到: