登录  /  注册
首页 > php教程 > php手册 > 正文

thinkphp上传zip和rar压缩包

不言
发布: 2018-05-17 11:40:44
原创
5734人浏览过

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);
    }
登录后复制

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

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号