ZIP & RAR 壓縮 (整理中)

資料來源 : Stackoverflow

// glob('temp/*.wav'); 
$files_to_zip = array_filter(glob('temp/*.wav'), 'is_file');
// echo "<pre>";
// var_export($files_to_zip);
// echo "</pre>";
// exit;

/**
 * zipin.php
 * 將多檔案壓縮成zip
 * 
 */
//if true, good; if false, zip creation failed 
$filename='myfile.zip'; 
$result = create_zip($files_to_zip,$filename);

if($result){
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($filename));
header('Content-disposition: attachment; filename='.$filename);
readfile($filename);
}

// // $files = array('audiotemp/1910027.wav'); //檔案名稱與路徑
// $zipname = 'file.zip'; //壓縮檔名
// $zip = new ZipArchive;
// $zip->open($zipname, ZipArchive::CREATE);
// foreach ($files as $file) {
//   $zip->addFile($file);
// }
// $zip->close();

//於下載頁面
//如果檔案存在
// if(is_file($zipname)){
// //下載zip檔案
// header('Content-Type: application/zip');
// header('Content-disposition: attachment; filename='.$zipname);
// header('Content-Length: ' . filesize($zipname));
// readfile($zipname);
// }else{
//     echo "檔案不存在";
// }

exit;


/* 生成zip 壓縮文件 */
function create_zip($files = array(),$destination = '',$overwrite = false) { 
  //if the zip file already exists and overwrite is false, return false 
  if(file_exists($destination) && !$overwrite) { return false; } 
  //vars 
  $valid_files = array(); 
  //if files were passed in... 
  if(is_array($files)) { 
    //cycle through each file 
    foreach($files as $file) { 
    //make sure the file exists 
    if(file_exists($file)) { 
      $valid_files[] = $file; 
    } 
  } 
  } 
  //if we have good files... 
  if(count($valid_files)) { 
    //create the archive 
    $zip = new ZipArchive(); 
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
    return false; 
  } 
  //add the files 
  foreach($valid_files as $file) { 
    $file_info_arr= pathinfo($file); 
    $zip->addFile($file,$file_info_arr['basename']);//去掉層級目錄 
  } 
    //debug 
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; 

    //close the zip -- done! 
    $zip->close(); 

    //check to make sure the file exists 
    return file_exists($destination); 
  } 
  else
  { 
    return false; 
  } 
}

results matching ""

    No results matching ""