


How to solve the problem of garbled Chinese file names in PHP compression
The solution to the garbled Chinese file name in PHP compression: first transcode the Chinese compressed package name using the iconv function; then process the zip file through the ZipArchive object.
The operating environment of this article: Windows 10 system, PHP version 7.1, DELL G3 computer
PHP decompresses the ZIP compressed package to solve the problem of Chinese garbled characters
Fix the Chinese garbled problem of native ZipArchive
The Chinese zip archive name and Chinese file name will cause PHP's native ZipArchive to be garbled, causing the extractTo method to fail:
<?php $zip = new \ZipArchive; $zipfile = "./test.zip"; $res = $zip->open($zipfile); $toDir = "./test"; $s = $zip->extractTo($toDir); print_r(scandir($toDir)); //作者:Dorm_Script //链接:http://www.jianshu.com/p/3efa924fd809
Referring to a blog, the original solution is:
<?php $zip = new \ZipArchive; $zipfile = "./test.zip"; $res = $zip->open($zipfile); $toDir = "./test"; if(!file_exists($toDir)) { mkdir($toDir); } $docnum = $zip->numFiles; for($i = 0; $i < $docnum; $i++) { $statInfo = $zip->statIndex($i); if($statInfo['crc'] == 0) { //新建目录 mkdir($toDir.'/'.substr($statInfo['name'], 0,-1)); } else { //拷贝文件 copy('zip://'.$zipfile.'#'.$statInfo['name'], $toDir.'/'.$statInfo['name']); } } print_r(scandir($toDir)); //作者:Dorm_Script //链接:http://www.jianshu.com/p/3efa924fd809
But when $zipfile is in Chinese encoding, that is, when the name of the compressed package is in Chinese, decompression will still fail.
At this time, use the iconv function to transcode the Chinese compressed package name and then use it:
//zip文件名 $fileName = '中文.zip'; //zip文件相对服务器根目录的保存路径 $uploads_dir = "/Public/zipUpload"; //zip文件完整的保存路径 $zipName = $_SERVER['DOCUMENT_ROOT'].$uploads_dir.'/'.$fileName; //将目标路径名称赋值为fileName最后的'.zip'四个字符之外的全部字符构成的字符串 $toDir = $_SERVER['DOCUMENT_ROOT'].$uploads_dir.'/'.substr($fileName,0,strlen($fileName)-4); $zip = new \ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表示处理的zip文件名。 如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE */ $res = $zip->open(iconv ( 'UTF-8', 'GB2312', $zipName)); if ($res === TRUE){ if (!is_dir(iconv ( 'UTF-8', 'GB2312', $toDir))) { mkdir(iconv ( 'UTF-8', 'GB2312', $toDir), 0777, true); } //$zip->extractTo($toDir); $docnum = $zip->numFiles; for($i = 0; $i < $docnum; $i++) { $statInfo = $zip->statIndex($i); if($statInfo['crc'] == 0) { //新建目录 mkdir(iconv ( 'UTF-8', 'GB2312', $toDir.'/'.$statInfo['name']), 0777, true); } else { //拷贝文件,特别的改动,iconv的位置决定copy能不能work if(copy('zip://'.iconv ( 'UTF-8', 'GB2312', $zipName).'#'.$statInfo['name'], iconv ( 'UTF-8', 'GB2312', $toDir.'/'.$statInfo['name'])) == false){ echo 'faild to copy'; } } } print_r(scandir(iconv ( 'UTF-8', 'GB2312',$toDir))); $zip->close();//关闭处理的zip文件 } else{ echo 'failed, code:'.$res.'<br>'; }
The output of the above code:
Array ( [0] => . [1] => .. [2] => �� [3] => Ҫ )
The directory of the compressed package:
中文.zip
├── No
│ └── Want
│ └── dance.txt
└── To
└── dance1.txt
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of garbled Chinese file names in PHP compression. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
