Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5024685
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2008-12-18 11:46:01

 

<?php

class DownLoad
{
    var $url;//远程文件地址

    var $file_name = "hdwiki.zip";//下载来的文件名称

    var $save_path = "./updatefile";//下载到本地的文件路径

    var $localfile;//下载到本地文件的路径和名称

    var $warning;//警告信息

    var $redown=0;//是否重新下载


    /*初始化*/
    function setUrl($url)
    {
         if(!empty($url))$this->url = $url;
    }

    function setFileName($file_name)
    {
     if(!empty($file_name))$this->file_name = $file_name;
    }

    function setSavePath($save_path)
    {
     if(!empty($save_path))$this->save_path = $save_path;
    }

    function setRedown($redown)
    {
     if(!empty($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);
        }
   }
   
    /* 检查URL合法性函数 */
    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;
             //exit("Error: 本地文件 ". $this->localfile ." 已经存在,请删除或改名后重新运行本程序.");

            }
        }

        //打开远程文件

        $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;
     }

     //下载远程文件

     //echo "正在下载远程文件,请等待";

     while (!feof($fp)){
     $tmpfile .= fread($fp, 1024);
     //echo strlen($tmpfile);

     }
       //保存文件到本地

       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;
    }
}
?>

阅读(1687) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~