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['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); }
一个新手的思路,大家多多批评指教……
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
3 몇 주 전ByDDD
Windows 11 KB5054979의 새로운 기능 및 업데이트 문제를 해결하는 방법
2 몇 주 전ByDDD
Will R.E.P.O. 크로스 플레이가 있습니까?
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

드림위버 CS6
시각적 웹 개발 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기
