thinkphp 上傳zip和rar壓縮包,然后php解壓zip和rar,在依次上傳解壓出的文件
以下思路是我個(gè)人的一些想法,有更好的方法希望大家分享交流……
1.創(chuàng)建一個(gè)表單?
<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); //創(chuàng)建解壓目錄 $title = $_FILES['file_name']['name']; //上傳壓縮包名稱 $media_jl = array(); //創(chuàng)建一個(gè)空數(shù)組 $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); //判斷上傳文件個(gè)數(shù),上傳文件不能多于10個(gè) if ($wj_count > 10) { //清空解壓文件 $this->deldir($dir); this->error('上傳文件多于10個(gè)!'); } //文件上傳提交 if (!empty($array_file)) { foreach ($array_file as $k => $v) { //此處就使用tp的上傳或者自己的上傳方法…… } }else{ this->error('壓縮包為空!'); } }else{ //其他格式的文件根據(jù)自己實(shí)際情況上傳 }
3.解壓zip文件
public function unzip_file($file, $dir){ // 實(shí)例化對(duì)象 $zip = new ZipArchive() ; //打開zip文檔,如果打開失敗返回提示信息 if ($zip->open($file) !== TRUE) { die ("Could not open archive"); } //將壓縮文件解壓到指定的目錄下 $zip->extractTo($dir); //關(guān)閉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."."); //定義用于存儲(chǔ)文件名的數(shù)組 $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.清除解壓文件(注:這個(gè)清除文件的方法不能清除中文名稱的文件)
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); //刪除當(dāng)前文件夾: if(rmdir($dir)) { return true; } else { return false; } }
清除解壓文件及文件夾,我使用了指令清除,大家有什么好的方法給我推薦一哈
function deldir($dir){ exec('rd /s /q '.$dir); }
一個(gè)新手的思路,大家多多批評(píng)指教……

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

PHP variables store values ??during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above
