搜尋
首頁php教程php手册thinkphp上传zip和rar压缩包

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

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。