ConFoo Montreal 2026: Call for Papers

Voting

: five plus three?
(Example: nine)

The Note You're Voting On

theking2(at)king.ma
1 year ago
As others have pointed out ZipArchive::addFile() is asynchronous. If you want to restrict the time when adding a large number of files this is the way to do it:

<?php
//$files is an array with a horrendous amount of filenames.
//$fileCount the total number of files in $files e.q. count($files)
//$fileIndex is the file you want to start with
for(
$fileIndex = 1, $start = hrtime( true );
$fileIndex < $fileCount and $secondsElapsed < 5.0;
$fileIndex++
) {
$currentfile = $files[ $fileIndex ];

if(
file_exists( $currentfile ) ) {
$zip->addFile( $currentfile );
usleep(1);
$secondsElapsed = ( hrtime( true ) - $start ) / 1e+9;
}

}
?>

this will keep on adding files until the time out (5.0s) arises. Without usleep(1) it will just continue until the max_excecution_time is reached.

<< Back to user notes page

To Top