0

我正在寻找一种压缩文件夹内容的解决方案,包括文件夹中的所有子文件夹,但不包括主文件夹本身

我从这个函数开始,它将整个文件夹添加到 zip 存档中

function addFolderToZip($dir, $zipArchive){
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {

            //Add the directory
            $zipArchive->addEmptyDir($dir);

            // Loop through all the files
            while (($file = readdir($dh)) !== false) {

                //If it's a folder, run the function again!
                if(!is_file($dir . $file)){
                    // Skip parent and root directories
                    if( ($file !== ".") && ($file !== "..")){
                        addFolderToZip($dir . $file . "/", $zipArchive);
                    }

                }else{
                    // Add the files
                    $zipArchive->addFile($dir . $file);

                }
            }
        }
    }
}

但我看不到如何将函数更改为(也许使用第三个参数?)

$z = new ZipArchive();
$z->open("zips/new.zip", ZIPARCHIVE::CREATE);
addFolderToZip("unzipped/",$z);
$z->close();
4

1 回答 1

1

我知道了,我创建了一个参数 $localpath,设置为“”:

function add_folder_in_path($folder_path,$local_path,$z)
{
    $dh=opendir($folder_path);
    while (($file = readdir($dh)) !== false) 
     {
        if( ($file !== ".") && ($file !== ".."))
        {
        if (is_file($folder_path.$file))
            {

                    $z->addFile($folder_path.$file,$local_path.$file);
            }
        else
            {
                add_folder_in_path($folder_path.$file."/",$local_path.$file."/",$z);
            }
        }


     }
}

我是这样称呼它的

$z = new ZipArchive();
$z->open("zipped/new.zip", ZIPARCHIVE::CREATE);
echo "<br>";
add_folder_in_path("myzips/","",$z);
$z->close();    
于 2013-03-14T23:01:39.860 回答