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压缩中文乱码问题的详细内容。