Home >Backend Development >PHP Tutorial >How to determine the file size of a compressed package through PHP ZipArchive?
How to determine the file size of compressed packages through PHP ZipArchive?
Summary:
PHP's ZipArchive class is a very useful tool that can be used to create, modify and extract compressed packages. However, sometimes we may need to judge the file size in the compressed package so that we can be prepared when dealing with large files. In this article, we will learn how to determine the size of the files in the compressed package through PHP's ZipArchive class.
Steps:
$zip = new ZipArchive; if ($zip->open('path/to/your/archive.zip') === true) { // 压缩包打开成功 } else { // 压缩包打开失败 }
$fileInfo = $zip->statIndex(0); // 获取第一个文件的信息 $fileSize = $fileInfo['size']; // 获取文件大小
$fileName = $zip->getNameIndex(0); // 获取第一个文件的文件名
$zip = new ZipArchive; if ($zip->open('path/to/your/archive.zip') === true) { for ($i = 0; $i < $zip->numFiles; $i++) { $fileInfo = $zip->statIndex($i); $fileName = $zip->getNameIndex($i); $fileSize = $fileInfo['size']; echo "文件名:$fileName,文件大小:$fileSize bytes "; } $zip->close(); } else { echo '无法打开压缩包文件'; }
Notes:
Conclusion:
Through PHP's ZipArchive class, we can easily judge the size of the files in the compressed package and perform corresponding processing as needed. This is very important when working with archives containing large files. Hope this article helps you!
The above is the detailed content of How to determine the file size of a compressed package through PHP ZipArchive?. For more information, please follow other related articles on the PHP Chinese website!