How to zip a whole folder using PHP

前端 未结 15 2206
有刺的猬
有刺的猬 2020-11-22 08:48

I have found here at stackoveflow some codes on how to ZIP a specific file, but how about a specific folder?

Folder/
  index.html
  picture.jpg
  important.tx         


        
相关标签:
15条回答
  • 2020-11-22 09:24

    Create a zip folder in PHP.

    Zip create method

       public function zip_creation($source, $destination){
        $dir = opendir($source);
        $result = ($dir === false ? false : true);
    
        if ($result !== false) {
    
            
            $rootPath = realpath($source);
             
            // Initialize archive object
            $zip = new ZipArchive();
            $zipfilename = $destination.".zip";
            $zip->open($zipfilename, ZipArchive::CREATE | ZipArchive::OVERWRITE );
             
            // Create recursive directory iterator
            /** @var SplFileInfo[] $files */
            $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
             
            foreach ($files as $name => $file)
            {
                // Skip directories (they would be added automatically)
                if (!$file->isDir())
                {
                    // Get real and relative path for current file
                    $filePath = $file->getRealPath();
                    $relativePath = substr($filePath, strlen($rootPath) + 1);
             
                    // Add current file to archive
                    $zip->addFile($filePath, $relativePath);
                }
            }
             
            // Zip archive will be created only after closing object
            $zip->close();
            
            return TRUE;
        } else {
            return FALSE;
        }
    
    
    }
    

    Call the zip method

    $source = $source_directory;
    $destination = $destination_directory;
    $zipcreation = $this->zip_creation($source, $destination);
    
    0 讨论(0)
  • 2020-11-22 09:24

    I did some small improvement in the script.

      <?php
        $directory = "./";
        //create zip object
        $zip = new ZipArchive();
        $zip_name = time().".zip";
        $zip->open($zip_name,  ZipArchive::CREATE);
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($directory),
            RecursiveIteratorIterator::LEAVES_ONLY
        );
        foreach ($files as $file) {
            $path = $file->getRealPath();
            //check file permission
            if(fileperms($path)!="16895"){
                $zip->addFromString(basename($path),  file_get_contents($path)) ;
                echo "<span style='color:green;'>{$path} is added to zip file.<br /></span> " ;
            }
            else{
                echo"<span style='color:red;'>{$path} location could not be added to zip<br /></span>";
            }
        }
        $zip->close();
        ?>
    
    0 讨论(0)
  • 2020-11-22 09:25

    Try this:

    $zip = new ZipArchive;
    $zip->open('myzip.zip', ZipArchive::CREATE);
    foreach (glob("target_folder/*") as $file) {
        $zip->addFile($file);
        if ($file != 'target_folder/important.txt') unlink($file);
    }
    $zip->close();
    

    This will not zip recursively though.

    0 讨论(0)
提交回复
热议问题