国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP遠(yuǎn)程下載類

2024-09-04 11:44:57
字體:
供稿:網(wǎng)友
  1. <?php 
  2.  
  3. class download 
  4.     var $url;//遠(yuǎn)程文件地址 
  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合法性函數(shù) */ 
  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.     {//開源代碼Vevb.com 
  61.         //檢測變量 
  62.  
  63.         $this->localfile = $this->save_path."/".$this->file_name; 
  64.          if($this->url == "" || $this->localfile == ""){ 
  65.                  $this->warning = "error: 變量設(shè)置錯誤."
  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 ." 已經(jīng)存在! <a href='?action=download&redown=1' target='_self'>重新下載</a>"
  82.                 return $this->warning; 
  83.              //exit("error: 本地文件 ". $this->localfile ." 已經(jīng)存在,請刪除或改名后重新運行本程序."); 
  84.  
  85.             } 
  86.         } 
  87.  
  88.         //打開遠(yuǎn)程文件 
  89.  
  90.         $fp = fopen($this->url, "rb"); 
  91.         if (!$fp){ 
  92.             $this->warning = "error: 打開遠(yuǎn)程文件 "$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.      //下載遠(yuǎn)程文件 
  105.  
  106.      //echo "正在下載遠(yuǎn)程文件,請等待"; 
  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. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 株洲市| 宁乡县| 子洲县| 通许县| 鄱阳县| 安达市| 盈江县| 区。| 齐河县| 澜沧| 汤原县| 东源县| 资源县| 晋城| 宜良县| 揭东县| 南宁市| 班戈县| 宁陵县| 封开县| 修水县| 平凉市| 广宁县| 凤翔县| 宁安市| 安阳市| 武冈市| 哈巴河县| 旌德县| 驻马店市| 乳山市| 三门县| 枣庄市| 河西区| 万全县| 乡城县| 沅江市| 富顺县| 台北县| 千阳县| 岫岩|