Arşivde boş bir dizin ekler

1 Cevap

i arşivde boş bir dizin ekler istiyorum.

ZipArchive::addEmptyDir

this function add it but i the root only

i eklemek istiyorum varsayıyorum

/dir/dir/

i cant

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    if($zip->addEmptyDir('newDirectory')) {
        echo 'Created a new root directory';
    } else {
        echo 'Could not create the directory';
    }
    $zip->close();
} else {
    echo 'failed';
}
?>

this add it in the root how i can add the new dir in sub dir

1 Cevap

// Function to recursively add a directory,
// sub-directories and files to a zip archive
function addFolderToZip($dir, $zipArchive, $zipdir = ''){
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, $zipdir . $file . "/");
                }

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

            }
        }
    }
}
}

Source: php.net