PHP script for remotely downloading a ZIP file, extracting it, and overwriting files into a specified directory; if the specified directory does not exist, it will be created; if the directory already exists, it will be emptied. The extracted ZIP file contains files with Chinese filenames!
I previously wrote an article titled"PHP: Remote download of a ZIP file, extract it, and overwrite it into a specified directory.You can refer to this, but after using it, I discovered a problem!
Code issue
That is, if the target directory does not exist, an error will be raised!
During the extraction process, an error occurred because the compressed file had a Chinese filename!
Of course, we could simply use the @ symbol to mask the first error directly, but after thinking about it, I still think it's better to make a change!
Problem Reflection
This time, let's refine it a bit; you can refer to this article:PHP-based archive extraction/compression tool; the archive contains a file with a Chinese name – why does an error occur after extraction?》
Here, we'll combine these two examples and then optimize the code function!
code implementation
The following example code demonstrates how to remotely download a ZIP file, extract it, and overwrite files in a specified directory; if the specified directory does not exist, it will be created; if the directory already exists, it will be emptied. The extracted ZIP file contains files with Chinese filenames:
$remoteZipUrl = 'https://example.com/remote.zip'; // 远程zip文件的URL
$localDir = '/path/to/local/dir'; // 本地目录的路径
// 如果本地目录不存在,创建目录
if (!file_exists($localDir)) {
mkdir($localDir, 0777, true);
}
// 如果本地目录存在,清空目录
if (file_exists($localDir) && is_dir($localDir)) {
$files = glob($localDir . '/*'); // 获取目录下的所有文件
foreach ($files as $file) {
if (is_file($file)) {
unlink($file); // 删除文件
}
}
}
// 下载zip文件到本地临时文件
$tempFile = tempnam(sys_get_temp_dir(), 'zip');
$fp = fopen($tempFile, 'w');
$ch = curl_init($remoteZipUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
// 解压zip文件到本地目录
$zip = new ZipArchive;
$res = $zip->open($tempFile);
if ($res === TRUE) {
$zip->setOptions(array('default_charset' => 'UTF-8')); // 设置文件名编码方式为 UTF-8
$zip->extractTo($localDir); // 解压缩到指定目录
$zip->close();
echo '解压缩完成';
} else {
echo '解压缩失败';
}
// 删除本地临时文件
unlink($tempFile);In this example, first check whether the local directory exists; if it does not exist, then use it. mkdir() The function creates a directory; if it already exists, it will be used. glob() 和 unlink() Clear directory.
Then, use it. tempnam() The function creates a temporary file for downloading a remote ZIP file. Use. curl The function downloads a ZIP file from a remote URL to a local temporary file.
Next, use. ZipArchive Open a local temporary file and set the file name encoding to UTF-8. Finally, call the function. extractTo() Method: Decompress the ZIP file into the specified directory. Once decompression is complete, delete the local temporary files.
Note: Before using this code, ensure that it is already installed on the server. ZipArchive Extend and curl expand.
function encapsulation
This function takes as parameters the URL of the remote ZIP file and the path to the local directory. It will check whether the local directory exists; if it does not, it will use the default path. mkdir() The function creates a directory; if it already exists, it will be used. glob() 和 unlink() Clear directory.
Then, use it. tempnam() The function creates a temporary file for downloading a remote ZIP file. Use. curl The function downloads a ZIP file from a remote URL to a local temporary file.
Next, use. ZipArchive Open a local temporary file and set the file name encoding to UTF-8. Finally, call the function. extractTo() Method: Decompress the ZIP file into the specified directory. Once decompression is complete, delete the local temporary files.
If the decompression is successful, the function will return. trueOtherwise, return. false。
Error:
An error occurred after running the function!!!
If you encounter any errors, please read these three articles of mine!!
《Fatal error: Call to undefined method ZipArchive::setOptions() – Solution!》
《How to check if ZipArchive is installed in your PHP environment?》
final project
If implementing the solution does not resolve the ZipArchive extension issue, then you may want to read this article!
《PclZip serves as a replacement for ZipArchive, resolving PHP error issues!》
Of course, if your server supports ZipArchive and the above function code is working correctly, you may skip this section.