php压缩中文乱码的解决方法:1、通过“composer require nelexa/zip”安装PhpZip;2、打开“ZipFile.php”;3、找到“extractTo”方法,将“is_writable”函数改掉或去掉;4、转换一下编码格式即可。
本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。
怎么解决php压缩中文乱码问题?
解决php使用ZipArchive解压时中文乱码问题(纯php,绕开ZipArchive)
解决php使用ZipArchive解压时中文乱码问题
使用php自带的ZipArchive来解压带中文文件名压缩包时会造成乱码,现象如下:
网上查阅基本上给出的答案大同小异,自己照着同样的方法试了都不能解决,下图是网上给出的方案:
经过摸索终于找到了解决方案,那就是弃用ZipArchive,选择其他的途径,经过比较我选择了“PhpZip”,优点是:纯php(不需要扩展和类),下面介绍安装方法:
composer安装:
composer require nelexa/zip
如果选择版本的话:composer require nelexa/zip:^3.0 (3.0版本号)
使用方法:
//解压文件 $fileAddess = "压缩包地址"; $toDir = "解压目录"; $zipFile = new \PhpZip\ZipFile(); $zipFile->openFile($fileAddess) // open archive from file ->extractTo($toDir); // extract files to the specified directory $zipFile->close();
注释:(**如果安装好使用正常请忽略下面的注释内容**)
1.我的压缩包放在共享盘里面连接的是smb地址,可能会出现报错:"Destination is not writable directory",这个坑已经踩过:打开ZipFile.php这个文件,找到“extractTo”方法,将“is_writable”函数改掉,或者去掉,然后再转换一下编码格式!,下面有注释的是我修改的内容!
/** * Extract the archive contents * * Extract the complete archive or the given files to the specified destination. * * @param string $destination Location where to extract the files. * @param array|string|null $entries The entries to extract. It accepts either * a single entry name or an array of names. * @return ZipFile * @throws ZipException */ public function extractTo($destination, $entries = null) { if (!file_exists($destination)) { throw new ZipException("Destination " . $destination . " not found"); } if (!is_dir($destination)) { throw new ZipException("Destination is not directory"); } $is_really_writable = $this->is_really_writable($destination); if (!$is_really_writable) { throw new ZipException("Destination is not writable directory"); } /** * @var ZipEntry[] $zipEntries */ if (!empty($entries)) { if (is_string($entries)) { $entries = (array)$entries; } if (is_array($entries)) { $entries = array_unique($entries); $flipEntries = array_flip($entries); $zipEntries = array_filter( $this->centralDirectory->getEntries(), function ($zipEntry) use ($flipEntries) { /** * @var ZipEntry $zipEntry */ return isset($flipEntries[$zipEntry->getName()]); } ); } } else { $zipEntries = $this->centralDirectory->getEntries(); } foreach ($zipEntries as $entry) { /******************************王天佑添加的逻辑start************************************/ header("Content-type:text/html;charset=bgk"); $entry_getName = iconv('GB2312', 'UTF-8//ignore',$entry->getName()); //header("Content-type:text/html;charset=utf-8"); /******************************王天佑添加的逻辑start************************************/ $file = $destination . DIRECTORY_SEPARATOR . $entry_getName; if ($entry->isDirectory()) { if (!is_dir($file)) { if (!mkdir($file, 0755, true)) { throw new ZipException("Can not create dir " . $file); } chmod($file, 0755); touch($file, $entry->getTime()); } continue; } $dir = dirname($file); if (!is_dir($dir)) { if (!mkdir($dir, 0755, true)) { throw new ZipException("Can not create dir " . $dir); } chmod($dir, 0755); touch($dir, $entry->getTime()); } if (file_put_contents($file, $entry->getEntryContent()) === false) { throw new ZipException('Can not extract file ' . $entry_getName); } touch($file, $entry->getTime()); } return $this; } //王天佑加的新逻辑,判断目录是否可写 public function is_really_writable($file){ if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE) { return is_writable($file); } if (is_dir($file)) { $file = rtrim($file, '/') . '/' . md5(mt_rand(1,100) . mt_rand(1,100)); if (($fp = @fopen($file, "w+")) === FALSE) { return FALSE; } fclose($fp); @chmod($file, 0777); @unlink($file); } elseif (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) { fclose($fp); return FALSE; } return TRUE; }
推荐学习:《PHP视频教程》
以上是怎么解决php压缩中文乱码问题的详细内容。更多信息请关注PHP中文网其他相关文章!

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3汉化版
中文版,非常好用