lundi 30 novembre 2015

Zipping random files in PHP

Hi I am trying to collect a number of random files from a folder on my server (./uploads) and zip them so the user can download. I think I am pretty close but I could use some help. I am getting a "503 backend fetch" error everytime and i can not pinpoint my issue. Thank you!!!

<?php 
$rootPath = realpath('./uploads');
//make zip file
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$nfiles = glob($rootPath.'*.{aiff}', GLOB_BRACE);     
$files = $nfiles[array_rand($nfiles,20)]; 
//-------------------------------------------//
foreach ($files as $file)
 { 
 if (!$file->isDir())
{
    // Get path for current file
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);  
    // Add file to zip
    $zip->addFile($filePath, $relativePath);
}
 }
 //-------------------------------------------//
 $zip->close();
 header('Content-Type: application/zip');
 header('Content-disposition: attachment; filename='.'generated            
 sounds.zip');
 header('Content-Length: ' . filesize('file.zip'));
 readfile('file.zip');
  if(file_exists('file.zip')){
  unlink('file.zip');
 }
?>




Aucun commentaire:

Enregistrer un commentaire