How to zip a whole folder using PHP

前端 未结 15 2566
有刺的猬
有刺的猬 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

    I did some small improvement in the script.

      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 "{$path} is added to zip file.
    " ; } else{ echo"{$path} location could not be added to zip
    "; } } $zip->close(); ?>

提交回复
热议问题