gpt4 book ai didi

php zipArchive 只解压缩某些扩展

转载 作者:行者123 更新时间:2023-12-05 08:59:41 30 4
gpt4 key购买 nike

我需要解压缩上传的内容。但是为了安全起见,必须验证这些文件只是图像文件,这样就无法将 php 添加到 zip 中,然后再运行它。

在进行解压缩时,我还需要保留文件结构。

$zip->extractTo($save_path . $file_name, array('*.jpg','*.jpeg','*.png','*.gif') );

不返回空值。是否有我可以为此使用的参数,或者我必须使用正则表达式循环遍历 zip 文件以匹配扩展名并创建文件夹并使用代码保存文件?

谢谢

最佳答案

来自 php.net , 处理 .txt 文件

<?php 
$value="test.zip";
$filename="zip_files/$value";
$zip = new ZipArchive;
if ($zip->open($filename) === true) {
echo "Generating TEXT file.";
for($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
if(preg_match('#\.(txt)$#i', $entry))
{
////This copy function will move the entry to the root of "txt_files" without creating any sub-folders unlike "ZIP->EXTRACTO" function.
copy('zip://'.dirname(__FILE__).'/zip_files/'.$value.'#'.$entry, 'txt_files/'.$value.'.txt');
}
}
$zip->close();
}
else{
echo "ZIP archive failed";
}
?>

关于php zipArchive 只解压缩某些扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387993/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com