Heim  >  Artikel  >  php教程  >  thinkphp上传zip和rar压缩包

thinkphp上传zip和rar压缩包

不言
不言Original
2018-05-17 11:40:445934Durchsuche

thinkphp 上传zip和rar压缩包,然后php解压zip和rar,在依次上传解压出的文件
以下思路是我个人的一些想法,有更好的方法希望大家分享交流……
1.创建一个表单 

<form name="theForm" id="theForm" action="/index.php/Article/add" method="post" enctype="multipart/form-data">
上传文件:<input id="file_name" type="file" name="file_name">
<input id="submit" name="submit" value="提交" type="submit">
<form>

2.接下来就到php的处理 

public function index(){
     //解压文件所保存的目录
     $dir = "D:\jl_zip";
     if (file_exists($dir) == true) {
          //清空解压文件
          $this->deldir($dir);
     }
     mkdir($dir);   //创建解压目录
     $title = $_FILES[&#39;file_name&#39;][&#39;name&#39;];  //上传压缩包名称
     $media_jl = array();  //创建一个空数组
     
     $file = $_FILES[&#39;file_name&#39;][&#39;tmp_name&#39;]; //需要压缩的文件[夹]路径
     
     $type_wj = pathinfo($title, PATHINFO_EXTENSION); //获取文件类型
     //判断文件类型
     if(strtolower($type_wj) == "zip" || strtolower($type_wj) == "rar"){
                if(strtolower($type_wj) == "zip"){
                    //解压zip文件
                    $this->unzip_file($file,$dir); 
                }else{
                    //解压rar文件
                    $this->unrar($file,$dir);
                }
                //获取解压后的文件
                $array_file = $this->loopFun($dir);
                $wj_count = count($array_file);
                //判断上传文件个数,上传文件不能多于10个
                if ($wj_count > 10) {
                    //清空解压文件
                    $this->deldir($dir);
                    this->error(&#39;上传文件多于10个!&#39;);
                }
                //文件上传提交
                if (!empty($array_file)) {
                    foreach ($array_file as $k => $v) {
                       //此处就使用tp的上传或者自己的上传方法……
                    }
                }else{
                    this->error(&#39;压缩包为空!&#39;);
                }
       }else{
              //其他格式的文件根据自己实际情况上传
       }

3.解压zip文件  

public function unzip_file($file, $dir){ 
        // 实例化对象 
        $zip = new ZipArchive() ; 
        //打开zip文档,如果打开失败返回提示信息 
        if ($zip->open($file) !== TRUE) { 
          die ("Could not open archive"); 
        } 
        //将压缩文件解压到指定的目录下 
        $zip->extractTo($dir); 
        //关闭zip文档 
        $zip->close();  
    }

4.解压rar文件    

public function unrar($file,$dir){
        $obj = new com("wscript.shell");
        if($obj){
            $obj->run(&#39;winrar x &#39;.$file.&#39; &#39;.$dir, 0, true);
            return true;
        }else{
            return false;
        }
        $obj->Quit(); 
        $obj->Release(); 
        $obj = null; 
    }

5.获取解压文件    

public function loopFun($dir)  
    {  
        $handle = opendir($dir.".");
        //定义用于存储文件名的数组
        $array_file = array();
        while (false !== ($file = readdir($handle)))
        {
            if ($file != "." && $file != "..") {
                $array_file[] = $dir.&#39;/&#39;.$file; //输出文件名
            }
        }
        closedir($handle);
        return $array_file;
        //print_r($array_file);
    }

6.清除解压文件(注:这个清除文件的方法不能清除中文名称的文件)    

function deldir($dir) {
        //先删除目录下的文件:
        $dh=opendir($dir);
        while ($file=readdir($dh)) {
            if($file!="." && $file!="..") {
                $fullpath=$dir."/".$file;
                    if(!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    deldir($fullpath);
              }
            }
        }
        closedir($dh);
        //删除当前文件夹:
        if(rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }

清除解压文件及文件夹,我使用了指令清除,大家有什么好的方法给我推荐一哈   

function deldir($dir){
        exec(&#39;rd /s /q &#39;.$dir);
    }

一个新手的思路,大家多多批评指教……

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Pigcms源码分享 精致UINächster Artikel:PHP执行SVN更新