note 107550 added to ziparchive.addfile

From: Date: Thu, 16 Feb 2012 20:25:05 +0000
Subject: note 107550 added to ziparchive.addfile
Groups: php.notes 
Request: Send a blank email to php-notes+get-186180@lists.php.net to get a copy of this message
Note that using addFile() will change the order of the files within the zip, in fact within the index of the zip. Does not matter much, except if you loop the index and use addFile() within that loop : it will likely give messy results. Example : <?php $zip = new ZipArchive; if ($zip->open('somefile.zip') === TRUE) { for ($i = 0; $i < $zip->numFiles; $i++) { if (forsomereason()) { addFile('./somenewfile.ext', $zip->getNameIndex($i)); } } } $zip->close(); ?> This code may loop for ever, depending on your forsomereason() function, or at least you're at risk. Try something like this instead : <?php $zip = new ZipArchive; if ($zip->open('somefile.zip') === TRUE) { for ($i = 0; $i < $zip->numFiles; $i++) { if (forsomereason()) { $couples[]=array('filename'=>'./somenewfile.ext','localname'=>$zip->getNameIndex($i)); } } } foreach ($couples as $couple) $zip->addFile($couple['filename'],$couple['localname']); $zip->close(); ?> Hope it helps ;-) ---- Server IP: 195.221.21.36 Probable Submitter: 24.222.137.204 ---- Manual Page -- http://www.php.net/manual/en/ziparchive.addfile.php Edit -- https://master.php.net/note/edit/107550 Del: integrated -- https://master.php.net/note/delete/107550/integrated Del: useless -- https://master.php.net/note/delete/107550/useless Del: bad code -- https://master.php.net/note/delete/107550/bad+code Del: spam -- https://master.php.net/note/delete/107550/spam Del: non-english -- https://master.php.net/note/delete/107550/non-english Del: in docs -- https://master.php.net/note/delete/107550/in+docs Del: other reasons-- https://master.php.net/note/delete/107550 Reject -- https://master.php.net/note/reject/107550 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186180) next »