在PHP中压缩随机文件

时间:2015-11-28 07:50:50

标签: php random download zip directory

您好我正在尝试从我服务器上的文件夹中收集大量随机文件(./uploads)并压缩它们以便用户可以下载。我想我很接近,但我可以使用一些帮助。我正在获得" 503后端获取"每次都错误,我无法确定我的问题。谢谢!!!

<?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');
 }
?>

0 个答案:

没有答案