homer.favenir Posted September 30, 2009 Share Posted September 30, 2009 hi, i need to compress or zip a folder or directory and here is my script $dir = "test"; $dir2 = scandir($dir); $zip = new ZipArchive; $r = $zip->open('test.zip', ZipArchive::CREATE); foreach($dir2 as $item){ if($item == "." || $item == ".."){ //do nothing }else{ $r = $zip->addFile($item,'item'); echo $item; } } but it's not zipping my folder but when i tried this one $zip = new ZipArchive; $r = $zip->open('test.zip', ZipArchive::CREATE); $r = $zip->addFile('testfile','testfile'); its working. i cant make the array working in ziparchive function please advice tia Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/ Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 <?php class Zipper extends ZipArchive { public function addDir($path) { print 'adding ' . $path . '<br>'; $this->addEmptyDir($path); $nodes = glob($path . '/*'); foreach ($nodes as $node) { print $node . '<br>'; if (is_dir($node)) { $this->addDir($node); } else if (is_file($node)) { $this->addFile($node); } } } } // class Zipper this is code straight from the addFile method's reference page the code was made public by "peter at boring dot ch" its not my code, but it would solve your problem Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927417 Share on other sites More sharing options...
homer.favenir Posted September 30, 2009 Author Share Posted September 30, 2009 its not working <?php $dir = "test"; //folder with 2 files class Zipper extends ZipArchive { public function addDir($path) { print 'adding ' . $path . '<br>'; $this->addEmptyDir($path); $nodes = glob($path . '/*'); foreach ($nodes as $node) { print $node . '<br>'; if (is_dir($node)) { $this->addDir($node); } else if (is_file($node)) { $this->addFile($node); } } } } $zip = new Zipper(); $zip->addDir($dir); ?> Warning: ZipArchive::addEmptyDir() [function.ZipArchive-addEmptyDir]: Invalid or unitialized Zip object in C:\xampp\htdocs\canpages\test\testzip.php on line 23 test/test1.php line 23 is $this->addEmptyDir($path); Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927422 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 $zip = new Zipper; $zip->open('theZipName.zip',Zipper::CREATE); $zip->addDir('test'); Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927427 Share on other sites More sharing options...
homer.favenir Posted September 30, 2009 Author Share Posted September 30, 2009 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927430 Share on other sites More sharing options...
homer.favenir Posted September 30, 2009 Author Share Posted September 30, 2009 is there a way i can indicate the target location for saved zip file, because it is saving in the directory of the sript tia Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927433 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 in the open method you'd specify the whole path.. for example: C:\myZip.zip and it'll save to C:\ but if you just do myZip.zip it'll save into the directory which contains the active php script Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927437 Share on other sites More sharing options...
homer.favenir Posted September 30, 2009 Author Share Posted September 30, 2009 thank you so much, youre a life saver Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927440 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 Nooo I wish, I'd want to be one of the gummy ones, and pink! they're the best lol. Please 'solve' the topic Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-927446 Share on other sites More sharing options...
homer.favenir Posted October 1, 2009 Author Share Posted October 1, 2009 hi, the zipping of file is ok but it also zips the root folder class Zipper extends ZipArchive { public function addDir($path) { //print 'adding ' . $path . '<br>'; $this->addEmptyDir($path); $nodes = glob($path . '/*'); foreach ($nodes as $node) { //print $node . '<br>'; if (is_dir($node)) { $this->addDir($node); } else if (is_file($node)) { $this->addFile($node); } } } } $dir = "\\\\asecasiagsd\\AGS\\AGS\\WIP\\RECSCHART\\"; $folder = "\\\\asecasiagsd\\AGS\\AGS\\WIP\\RECSCHART\\".$artid; $dest = $dir; $zip = new Zipper; $zip->open($dest.$artid.'.zip',Zipper::CREATE); $zip->addDir($dir.$artid); $zip->close(); when i unzip it, it has a directory of ags/wip/recschart/$artid/all files i need the folder $artid/all files only please help TA Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928375 Share on other sites More sharing options...
homer.favenir Posted October 2, 2009 Author Share Posted October 2, 2009 anyone please? Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928814 Share on other sites More sharing options...
RussellReal Posted October 2, 2009 Share Posted October 2, 2009 I've modified the code above that I gave you from the poster on php.net and have come up with something pretty sweet, hope it solves your problem <?php class Zipper extends ZipArchive { public function addIntoDir($path,$name = false,$into = false) { $path = trim($path,'/'); if ($name === false) $name = $path; $this->addEmptyDir($e = ($into.'/'.$name)); $nodes = glob($path . '/*'); foreach ($nodes as $node) { $x = explode('/', $node); if (is_dir($node)) { $this->addIntoDir($node,$x[count($x) - 1],$path); } else if (is_file($node)) { $this->addFile($node,$path.'/'.$x[count($x) - 1]); } } } } $z = new Zipper; $z->open('zipToMake.zip',Zipper::CREATE); $z->addIntoDir('PathToTheDirectoryToZip','NameOfDirectoryInsideZipFile'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928835 Share on other sites More sharing options...
homer.favenir Posted October 2, 2009 Author Share Posted October 2, 2009 sorry to ask but is this the correct usage for the script? $dir = "//asecasiagsd/AGS/AGS/WIP/RECSCHART//"; $folder = $artid; $z = new Zipper; $z->open($dir.$artid.".zip",Zipper::CREATE); $z->addIntoDir($dir,$folder); $z->close(); thanks Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928855 Share on other sites More sharing options...
RussellReal Posted October 2, 2009 Share Posted October 2, 2009 what is $artid? you want the files to come from RECSCHART but you want it to show up in /$artid/? than yes thats how you'd do it Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928857 Share on other sites More sharing options...
homer.favenir Posted October 2, 2009 Author Share Posted October 2, 2009 i need to zip $dir = "//asecasiagsd/AGS/AGS/WIP/RECSCHART//"; $folder = $artid; $folder_to_zip = $dir.$folder; and save it to $dir.... thanks for the continous help, i just cant figure it out... Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928928 Share on other sites More sharing options...
nikes90 Posted October 2, 2009 Share Posted October 2, 2009 Hey, can you plz tell me how can I start a new topic? Thanks, Nikkh Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928931 Share on other sites More sharing options...
TeNDoLLA Posted October 2, 2009 Share Posted October 2, 2009 Hey, can you please tell me how can I start a new topic? Thanks, Nikkh Top right corner just before the topic list begins is few links including "New topic". Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928934 Share on other sites More sharing options...
homer.favenir Posted October 2, 2009 Author Share Posted October 2, 2009 or i will just use the rename function to move the save zip file? is there any better way? Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928935 Share on other sites More sharing options...
homer.favenir Posted October 2, 2009 Author Share Posted October 2, 2009 $dir = "d:\\asecmenu\sample\sample"; $z = new Zipper; $z->open('d:\\asecmenu\sample\zipToMake.zip',Zipper::CREATE); $z->addIntoDir($dir,$dir); i tried this in my localhost, and it is zipping the all folder i indicated in $dir from asecmenu\sample\sample\*.* so the zip file has three folders, asecmenu, sample and sample, but i want is just the last sample folder... Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-928938 Share on other sites More sharing options...
RussellReal Posted October 2, 2009 Share Posted October 2, 2009 <?php $dir = "d:\\asecmenu\sample\sample"; $z = new Zipper; $z->open('d:\\asecmenu\sample\zipToMake.zip',Zipper::CREATE); $z->addIntoDir($dir,'sample'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-929182 Share on other sites More sharing options...
homer.favenir Posted October 3, 2009 Author Share Posted October 3, 2009 he is zipping the whole directory in open method... Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-929429 Share on other sites More sharing options...
homer.favenir Posted October 6, 2009 Author Share Posted October 6, 2009 anyone please.... Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-931337 Share on other sites More sharing options...
homer.favenir Posted October 6, 2009 Author Share Posted October 6, 2009 hi, i need to zip a file and download it <?php //function to zip and force download the files using PHP function zipFilesAndDownload($file_names,$archive_file_name,$file_path) { //create the object $zip = new ZipArchive(); //create the file and throw the error if unsuccessful if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) { exit("cannot open <$archive_file_name>\n"); } //add each files of $file_name array to archive foreach($file_names as $files) { $zip->addFile($file_path.$files,$files); } $zip->close(); //then send the headers to foce download the zip file header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=$archive_file_name"); header("Pragma: no-cache"); header("Expires: 0"); readfile("$archive_file_name"); exit; } $file = "AG3437081ETB0902004"; $dir = scandir($file); $file_names = $dir; $archive_file_name = $file."2.zip"; $file_path = "C:/xampp/htdocs/laboratory/compress/".$file."/"; echo $file_path; zipFilesAndDownload($file_names,$archive_file_name,$file_path); ?> i dont know whats wrong with my script can anyone please kindly check it. TIA Quote Link to comment https://forums.phpfreaks.com/topic/176007-ziparchive/#findComment-931383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.