概述
在很多实际生产场景都需要批量上传、下载一些文件的处理,整理了使用PHP语言操作ZipArchive实践和实例,ZipArchive需要服务器上安装zlib库,php扩展中安装zip扩展。
服务器环境扩展
ZipArchive类库的PHP版本要求如下,另外php需要查看是否已经成功安装zip扩展,服务器上需要安装zlib包,具体查看方法在下面的代码段里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # ZipArchive 类版本要求,来自官网 # (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; # 输出结果 # Zip Extension Version: 1.15.6 |
实践
ZipArchive类,使用范围非常丰富,这篇博客里主要介绍上传和下载功能,先整理下载的实践实例,有几点需要特别注意的点:
- 目录和文件的权限,包括复制的源文件和目标文件
- 移动的文件夹一定要存在
- ZipArchive扩展所需要的zlib和zip扩展,注意版本的差异性
文件下载
文件下载相对比较容易,先创建一个空的zip包,在把需要压缩的文件添加进zip包里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //压缩包生成的路径,最后文件添加在这个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 '服务器错误' .PHP_EOL; } $filePath = '/server_images/data/劳务派遣协议.pdf' ; $fileSuffix = pathinfo ( $filePath ,PATHINFO_EXTENSION); // 输出 pdf $fileName = pathinfo ( $filePath , PATHINFO_FILENAME); // 输出 劳务派遣协议 $rename = 'stark_' . $fileName . '.' . $fileSuffix ; //新名字 #把路径 $filePath 生成到zip包中, $rename 是新的文件名 $zip ->addFile( $filePath , $rename ); # 创建目录的路径 $createPathName = '' ; $zip ->addEmptyDir( $createPathName ); $zip ->close(); $strFile = '劳务派遣协议.zip' ; header( "Content-type:application/zip" ); header( "Content-Disposition:attachment;filename=" . $strFile ); readfile( $destination ); |
文件上传
1、文件上传相对比较麻烦,首先要把文件移动到指定的目录下,demo中的例子是$file_path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $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 '请检查数据来源' .PHP_EOL; } |
2、对压缩包进行解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $destination = '/home/wwwroot/labor_con2.zip' ; $zip = new ZipArchive; if ( $zip ->open( $destination , ZIPARCHIVE::CREATE) !== true) { echo '服务器错误' .PHP_EOL; } //解压到目标目录 $extractDir $extractDir = '/home/wwwroot/zip' ; if (! is_dir ( $extractDir )) { mkdir ( $extractDir , 0777, true); } $zip ->extractTo( $extractDir ); $zip ->close(); |
3、把解压的文件移动到目标的资源文件夹里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $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 '文件复制成功了' .PHP_EOL; } } } |
到此这篇关于PHP操作ZipArchive实现文件上传下载功能的文章就介绍到这了,更多相关PHP ZipArchive文件上传下载内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!