欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

概述

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

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

ZipArchive類庫的PHP版本要求如下,另外php需要查看是否已經(jīng)成功安裝zip擴(kuò)展,服務(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

實(shí)踐

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

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

文件下載

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

//壓縮包生成的路徑,最后文件添加在這個(gè)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ù)器錯(cuò)誤'.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、文件上傳相對(duì)比較麻煩,首先要把文件移動(dòng)到指定的目錄下,demo中的例子是$file_path

$file_path = '/home/wwwroot/upload/';
if (!is_dir(dirname($file_path))) {
    mkdir(dirname($file_path), 0777, true);
}
//把文件移動(dòng)到$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 '請(qǐng)檢查數(shù)據(jù)來源'.PHP_EOL;
}

2、對(duì)壓縮包進(jìn)行解壓

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

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

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

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

3、把解壓的文件移動(dòng)到目標(biāo)的資源文件夾里

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

//把$extractToPath 移動(dòng)到 $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實(shí)現(xiàn)文件上傳下載功能的文章就介紹到這了,更多相關(guān)PHP ZipArchive文件上傳下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論