Home  >  Article  >  Backend Development  >  What to do if the folder is garbled when php compresses the file

What to do if the folder is garbled when php compresses the file

藏色散人
藏色散人Original
2022-10-28 09:22:301378browse

Solution to garbled folders when php compresses files: 1. Modify the "pclzip.php" class file with the content "mb_convert_encoding( $p_filedescr['stored_filename'],'GB2312','UTF-8' );"; 2. Without changing the class package file, just modify "iconv("GBK","UTF-8//IGNORE",$file);".

What to do if the folder is garbled when php compresses the file

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

What should I do if the folder is garbled when php compresses the file?

Solution to the problem of Chinese garbled characters in PHP PclZip

When using Pclzip, the file cannot be compressed/decompressed. After tracking the error message, it was found that the file/directory cannot be opened. However, the folder permissions are correct, but after printing the file path, I found that it was garbled. The reason for this problem is that the file name encoding in the zip under windows is gb2312, while php uses utf-8 encoding.

Method 1: The solution is to modify the pclzip.php class file:

Modify the compressed file part:

privAddFile method:

//$p_header['stored_filename'] = $p_filedescr['stored_filename'];
// 修改为下面一行
$p_header['stored_filename'] = mb_convert_encoding( $p_filedescr['stored_filename'],'GB2312','UTF-8');

Unzip the file Partial modification:

privExtractFile method:

$p_entry['filename'] = $p_path."/".$p_entry['filename'];
// 加入下面一行
$p_entry['filename'] = mb_convert_encoding($p_entry['filename'], 'UTF-8', 'gb2312');

Method 2: Without changing the class package file, you can use the code to decompress the garbled Chinese file name. The pseudo code is as follows:

$file_type = mb_detect_encoding($file,array('ASCII','GB2312','GBK','UTF-8','LATIN1','BIG5'));
if($file_type=='EUC-CN'){
    $file = iconv("GBK","UTF-8//IGNORE",$file);
}

$file is the normal file name after decompression, but the file name on the server in this method is still garbled, but $file is the normal Chinese file name in the molding process.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if the folder is garbled when php compresses the file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn