i'm looking to create a Zip file on the fly, while downloading it. Let me explain: I have some urls (that change every time) that come from remote servers and i would like to create a container for all (zip archive) so they can be downloaded at once, instead of downloading all files singularly.
Here is my code:
$files = array(
'http://example.com/file1.pdf'=> array('size'=>55050),
'http://example.com/file2.doc'=> array('size'=>10000),
'http://example.com/file3.txt'=> array('size'=>5000)
);
$total_size = 0;
foreach($files as $file=>$info){
$total_size = $total_size+(int)$info['size'];
}
ob_start();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="Test.zip"');
header('Content-Length: '.$total_size);
header("Content-Transfer-Encoding: binary");
header('Content-Encoding: chunked');
foreach($files as $url=>$info){
while (@ob_end_flush());
readfile($url);
}
ob_end_flush();
ob_end_clean();
exit;
This will generate (obviously) a corrupted archive... Can anyone help me? Thanks in advance.