初始環境
1.壓縮包格式為:zip
2.壓縮文件如下
3.控制器初始代碼
<?php
namespace app\zip\controller;
use think\Controller;
class Index extends Controller
{
//
public function index(){
// 獲取表單上傳文件 例如上傳了001.jpg
$file = request()->file('file');
// 移動到框架應用根目錄/uploads/ 目錄下
$info = $file->validate(['ext'=>'zip'])->move( '../uploads');
if($info){
// 成功上傳后 獲取上傳信息
// 輸出 jpg
echo $info->getExtension();
// 輸出 20160820/42a79759f284b767dfcb2a0197904287.jpg
echo $info->getSaveName();
// 輸出 42a79759f284b767dfcb2a0197904287.jpg
echo $info->getFilename();
}else{
// 上傳失敗獲取錯誤信息
echo $file->getError();
}
return "1111";
}
}
4.路由
直接就是/uploadZip
5.使用postman模擬前端提交結果如下
好了,現在基本的文件上傳已經完成了,現在做zip解壓
安裝zip擴展
window zip擴展下載地址
php 擴展總站:
PHP 5.2/5.3的Windows擴展索引站點:
在這里你可以找到諸如php_oci8.dll, php_memcache.dll, php_mongo.dll, php_apc.dll等常用的Windows擴展dll文件,也能找到一些相對冷門但是也很實用的php擴展,如php_oauth.dll, php_solr.dll等擴展
http://downloads.php.net/pierre/
PHP 5.3/5.4/5.5的Windows擴展索引站點:
那么你就需要到下面這個站點來找擴展了, 該站點能下載到支持php5.4/5.5的pecl擴展,
http://windows.php.net/downloads/pecl/releases/
所有版本的zip包
https://pecl.php.net/package/zip
下載對應的zip動態鏈接庫
例如:
php 7.0 (Thread Safety:如果是enable,則是Thread Safe(線程安全)版本;否則,就是None Thread Safe(非線程安全)版本)。
圖中就是線程安全版本
則下載這個版本的動態鏈接庫,如果是32位的就下載32位的
鏈接:https://pecl.php.net/package/zip/1.13.5/windows
配置開啟zip擴展
將解壓出來的php_zip.dll
文件放在php的擴展目錄下
找到php.ini
配置文件加入extension=php_zip.dll
,重啟apache
訪問phpinfo()查看
如上說明配置成功
解壓文件
在這里提供兩個封裝的方法,我將這兩個方法放在了common文件
這兩個方法轉自:https://www.cnblogs.com/guliang/p/11742297.html
/**
* 壓縮文件
* @param array $files 待壓縮文件 array('d:/test/1.txt','d:/test/2.jpg');【文件地址為絕對路徑】
* @param string $filePath 輸出文件路徑 【絕對文件地址】 如 d:/test/new.zip
* @return string|bool
*/
function zip($files, $filePath)
{
//檢查參數
if (empty($files) || empty($filePath)) {
return false;
}
//壓縮文件
$zip = new ZipArchive();
$zip->open($filePath, ZipArchive::CREATE);
foreach ($files as $key => $file) {
//檢查文件是否存在
if (!file_exists($file)) {
return false;
}
$zip->addFile($file, basename($file));
}
$zip->close();
return true;
}
/**
* zip解壓方法
* @param string $filePath 壓縮包所在地址 【絕對文件地址】d:/test/123.zip
* @param string $path 解壓路徑 【絕對文件目錄路徑】d:/test
* @return bool
*/
function unzip($filePath, $path)
{
if (empty($path) || empty($filePath)) {
return false;
}
$zip = new ZipArchive();
if ($zip->open($filePath) === true) {
$zip->extractTo($path);
$zip->close();
return true;
} else {
return false;
}
}
修改后的控制器代碼
<?php
namespace app\zip\controller;
use think\Controller;
class Index extends Controller
{
//
public function index(){
// 獲取表單上傳文件 例如上傳了001.jpg
$file = request()->file('file');
// 移動到框架應用根目錄/uploads/ 目錄下
$info = $file->validate(['ext'=>'zip'])->move( '../uploads');
if($info){
//該函數為自定義函數放在了common文件中
unzip('../uploads/'.$info->getSaveName(), '../uploads/');
}else{
// 上傳失敗獲取錯誤信息
echo $file->getError();
}
return "1111";
}
}
解壓后如圖