我想提取文件夹中的所有zip文件。所以请检查我的下面的片段并帮助我。
<?php
$path='C:\xampp\htdocs\m'; // folder name
if ($handle = opendir($path)){
while (false !== ($file = readdir($handle))){
if ('.' === $file) continue;
if ('..' === $file) continue;
//echo "filename: ".$file."<br />";
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE){
$zip->extractTo($path); // extract it to the path
$zip->close();
echo " Done! $file extracted to $dir"; // done
}
else{
echo "opps! I couldn't open $file"; // no
}
}
closedir($handle);
}
?>
答案 0 :(得分:0)
请查看下面的代码段。下面的脚本将找到文件夹中的所有zip文件并将其提取到动态创建的文件夹中。
<?php
//Zip file path`
$path = $_SERVER['DOCUMENT_ROOT'].'/m/';
foreach (glob($path."*.zip") as $filename) {
//Destination folder to save extract zip file
$dest = $path.basename($filename, ".zip");
//create folder to extract zip file in it.
mkdir($dest,"0777");
//Check wether the zip file exist or not.
if (file_exists($path.$filename)) {
//create object of ZipArchive class
$zip = new ZipArchive;
//Open zip file`
$res = $zip->open($path.$filename);
if ($res === TRUE){
//extract zip file in destination folder`
$zip->extractTo($dest); // extract it to the path
//close zip file after extracted.
$zip->close();
}
}
}
?>
答案 1 :(得分:0)
你可以使用glob();
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>