thinkphp 上传zip和rar压缩包,然后php解压zip和rar,在依次上传解压出的文件
以下思路是我个人的一些想法,有更好的方法希望大家分享交流……
1.创建一个表单
上传文件:
2.接下来就到php的处理public function index(){
//解压文件所保存的目录
$dir = "D:\jl_zip";
if (file_exists($dir) == true) {
//清空解压文件
$this->deldir($dir);
}
mkdir($dir); //创建解压目录
$title = $_FILES['file_name']['name']; //上传压缩包名称
$media_jl = array(); //创建一个空数组
$file = $_FILES['file_name']['tmp_name']; //需要压缩的文件[夹]路径
$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('上传文件多于10个!');
}
//文件上传提交
if (!empty($array_file)) {
foreach ($array_file as $k => $v) {
//此处就使用tp的上传或者自己的上传方法……
}
}else{
this->error('压缩包为空!');
}
}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('winrar x '.$file.' '.$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.'/'.$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('rd /s /q '.$dir);
}
一个新手的思路,大家多多批评指教……