mercredi 2 décembre 2015

Download random files as zip

I posted my code a few days back and I am now at this point with it. I have acquired the random files however, when they zip it becomes a zip.cpgz file after unzipping. I am sure that this has something to do with the way I used array in my loop but I am not quite sure of how to fix this. Any help would be awesome.

 <?php 
 //./uploads
$dir = "./uploads";
$nfiles = glob($dir.'*.{aiff}', GLOB_BRACE);     
$n=1;
while ($n<=10){
$n ++;
$arr[$n] = rand(2, sizeof($nfiles)-1);
print($arr[$n]);
print("   ");
}


$zip = new ZipArchive();
$zip_name = "zipfile.zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
    $error .= "* Sorry ZIP creation failed at this time";
}

foreach($arr as $file){
    $path = "./uploads".$file;
    $zip->addFromString(basename($path),  file_get_contents($path)); 
}

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zip_name);
readfile('zipfile.zip');

?>

Thanks, Nick

Also if you are kinda lost here is my website I am trying to implement it on. http://ift.tt/1RoMTUr (click the download button)

Thank you!




Aucun commentaire:

Enregistrer un commentaire