亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP操作ZipArchive實現(xiàn)文件上傳下載功能

 更新時間:2024年03月27日 09:38:53   作者:stark張宇  
在很多實際生產(chǎn)場景都需要批量上傳、下載一些文件的處理,本文將利用PHP?ZipArchive實現(xiàn)文件上傳下載功能,有需要的小伙伴可以參考一下

概述

在很多實際生產(chǎn)場景都需要批量上傳、下載一些文件的處理,整理了使用PHP語言操作ZipArchive實踐和實例,ZipArchive需要服務(wù)器上安裝zlib庫,php擴展中安裝zip擴展。

服務(wù)器環(huán)境擴展

ZipArchive類庫的PHP版本要求如下,另外php需要查看是否已經(jīng)成功安裝zip擴展,服務(wù)器上需要安裝zlib包,具體查看方法在下面的代碼段里。

# ZipArchive 類版本要求,來自官網(wǎng)
# (PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.1.0)

#查看是否安裝zlib包
yum list installed | grep zlib


php-fpm -m | grep zip
zip

$zipVersion = phpversion('zip');
echo "Zip Extension Version: " . $zipVersion.PHP_EOL;

# 輸出結(jié)果 
# Zip Extension Version: 1.15.6

實踐

ZipArchive類,使用范圍非常豐富,這篇博客里主要介紹上傳和下載功能,先整理下載的實踐實例,有幾點需要特別注意的點:

  • 目錄和文件的權(quán)限,包括復(fù)制的源文件和目標文件
  • 移動的文件夾一定要存在
  • ZipArchive擴展所需要的zlib和zip擴展,注意版本的差異性

文件下載

文件下載相對比較容易,先創(chuàng)建一個空的zip包,在把需要壓縮的文件添加進zip包里。

//壓縮包生成的路徑,最后文件添加在這個zip包中
$destination = '/home/wwwroot/testDemo.zip';

if (!file_exists(dirname($destination))) {
    mkdir(dirname($destination), 0777, true);
}

$zip = new ZipArchive;
if ($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
    echo '服務(wù)器錯誤'.PHP_EOL;
}

$filePath = '/server_images/data/勞務(wù)派遣協(xié)議.pdf';

$fileSuffix = pathinfo($filePath,PATHINFO_EXTENSION); // 輸出 pdf
$fileName = pathinfo($filePath, PATHINFO_FILENAME);   // 輸出 勞務(wù)派遣協(xié)議
$rename = 'stark_' . $fileName.'.'.$fileSuffix; //新名字

#把路徑$filePath 生成到zip包中,$rename是新的文件名
$zip->addFile($filePath,  $rename );

# 創(chuàng)建目錄的路徑
$createPathName = '';
$zip->addEmptyDir($createPathName);
$zip->close();

$strFile = '勞務(wù)派遣協(xié)議.zip';
header("Content-type:application/zip");
header("Content-Disposition:attachment;filename=" . $strFile);
readfile($destination);

文件上傳

1、文件上傳相對比較麻煩,首先要把文件移動到指定的目錄下,demo中的例子是$file_path

$file_path = '/home/wwwroot/upload/';
if (!is_dir(dirname($file_path))) {
    mkdir(dirname($file_path), 0777, true);
}
//把文件移動到$file_path目錄里
if( is_uploaded_file($_FILES['file']['tmp_name']) ) {
    $move_re = move_uploaded_file($_FILES['file']['tmp_name'], $file_path);

    if (!$move_re) {
        echo '上傳失敗'.PHP_EOL;
    }
}else{
    echo '請檢查數(shù)據(jù)來源'.PHP_EOL;
}

2、對壓縮包進行解壓

$destination = '/home/wwwroot/labor_con2.zip';

$zip = new ZipArchive;
if ($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
    echo '服務(wù)器錯誤'.PHP_EOL;
}

//解壓到目標目錄 $extractDir
$extractDir = '/home/wwwroot/zip';
if (!is_dir($extractDir)) {
    mkdir($extractDir, 0777, true);
}

$zip->extractTo($extractDir);
$zip->close();

3、把解壓的文件移動到目標的資源文件夾里

$zipName = 'labor_con2';
$realExtractDir = $extractDir.'/'.$zipName.'/';
$folders = scandir($realExtractDir);

//把$extractToPath 移動到 $targetSrc位置
$targetDir = '/server_images/data/target/';
if (!is_dir($targetDir)) {
    mkdir($targetDir, 0777, true);
}

foreach ( $folders as $file){
    if(!in_array($file,['.','..','.DS_Store'])){

        $sourceSrc = $realExtractDir.$file;
        $targetSrc = $targetDir.$file;

        if (file_exists($sourceSrc)) chmod($sourceSrc, 0755);
        if (file_exists($targetSrc)) chmod($targetSrc, 0755);

        $result = copy($sourceSrc, $targetSrc);
        if($result){
            echo '文件復(fù)制成功了'.PHP_EOL;
        }
    }
}

到此這篇關(guān)于PHP操作ZipArchive實現(xiàn)文件上傳下載功能的文章就介紹到這了,更多相關(guān)PHP ZipArchive文件上傳下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論