note 109618 added to class.ziparchive

From: Date: Thu, 02 Aug 2012 10:44:44 +0000
Subject: note 109618 added to class.ziparchive
Groups: php.notes 
Request: Send a blank email to php-notes+get-189579@lists.php.net to get a copy of this message
Hi there. I just wrote a little function to zip a whole folder while maintaining the dir-structure. I hope it might help someone. <?php function folderToZip($folder, &$zipFile, $subfolder = null) { if ($zipFile == null) { // no resource given, exit return false; } // we check if $folder has a slash at its end, if not, we append one $folder .= end(str_split($folder)) == "/" ? "" : "/"; $subfolder .= end(str_split($subfolder)) == "/" ? "" : "/"; // we start by going through all files in $folder $handle = opendir($folder); while ($f = readdir($handle)) { if ($f != "." && $f != "..") { if (is_file($folder . $f)) { // if we find a file, store it // if we have a subfolder, store it there if ($subfolder != null) $zipFile->addFile($folder . $f, $subfolder . $f); else $zipFile->addFile($folder . $f); } elseif (is_dir($folder . $f)) { // if we find a folder, create a folder in the zip $zipFile->addEmptyDir($f); // and call the function again folderToZip($folder . $f, $zipFile, $f); } } } } ?> Use it like this: <?php $z = new ZipArchive(); $z->open("test.zip", ZIPARCHIVE::CREATE); folderToZip("storeThisFolder", $z); $z->close(); ?> Have a good day! ---- Server IP: 212.124.37.9 Probable Submitter: 92.228.4.244 ---- Manual Page -- http://www.php.net/manual/en/class.ziparchive.php Edit -- https://master.php.net/note/edit/109618 Del: integrated -- https://master.php.net/note/delete/109618/integrated Del: useless -- https://master.php.net/note/delete/109618/useless Del: bad code -- https://master.php.net/note/delete/109618/bad+code Del: spam -- https://master.php.net/note/delete/109618/spam Del: non-english -- https://master.php.net/note/delete/109618/non-english Del: in docs -- https://master.php.net/note/delete/109618/in+docs Del: other reasons-- https://master.php.net/note/delete/109618 Reject -- https://master.php.net/note/reject/109618 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189579) next »