網頁

2013年7月2日 星期二

PHP ZIP file download

Creating ZIP File with PHP.

 
  $error = "";    //error holder
  if(isset($_POST['createpdf'])){
    $post = $_POST;   

    if(extension_loaded('zip')){  // Checking ZIP extension is available
 
        $zip = new ZipArchive();      // Load zip library 
        $zip_name = time().".zip";      // Zip name
        if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){   // Opening zip file to load files
          $error .=  "* Sorry ZIP creation failed at this time
";
        }

        $files_to_zip = glob('./files/*.jpg');  //glob(./files/*.*);

        //$path = '../../../upload/'.$_GET["dir"].'/'; 
        //$files_to_zip =glob("$path{*.jpg,*.wmv}", GLOB_BRACE); //副檔名多指定

        foreach($files_to_zip as $file){       
          $zip->addFile($file);      
        }

        $zip->close();
        if(file_exists($zip_name)){
          // push to download the zip
          header('Content-type: application/zip');
          header('Content-Disposition: attachment; filename="'.$zip_name.'"');
          readfile($zip_name);
          // remove zip file is exists in temp path
          unlink($zip_name);
        }
    }else
      $error .= "* You dont have ZIP extension
";

  }
 
 
進階:

php creating zips without path to files inside the zip

 
foreach($files_to_zip as $file){      
             $new_filename = substr($file,strrpos($file,'/') + 1);
             $zip->addFile($file,$new_filename);
 }
更進階:

Single File PHP Gallery 4.1.1

 
  function fillInfo(type, id)
  {
   if (!index || (type == 'img'))
   {

                             if (type == 'dir')
    {
                             
                               info += '".str_to_script(TEXT_TOTAL_DOWNLOAD)."

';

 
 if (($_GET["cmd"] == "dlt") and TEXT_TOTAL_DOWNLOAD)
 {
   
     sfpg_zip_dl(GALLERY, IMAGE, "image", TRUE);
     exit;
 }
 
    //zip整包下載
 function sfpg_zip_dl($image_dir, $image_file, $func, $download=FALSE)
 {   
  if ($func == "image")
  {
   if (!file_exists($image_path_file))
   {
    $image_path_file = GALLERY_ROOT . $image_dir . $image_file;


   }
   if ($download)
   {  
  
           $zip = new ZipArchive();      // Load zip library 
           $zip_name = time().".zip";      // Zip name
           if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){   // Opening zip file to load files
             $error .=  "* Sorry ZIP creation failed at this time
";
           }

           $path = './files/'.$_GET["dir"]; 
           $files_to_zip = glob($path.'/*.jpg');  //glob(./files/*.*);
           
           foreach($files_to_zip as $file){      
              $new_filename = substr($file,strrpos($file,'/') + 1);  //creating zips without path to files inside the zip
              $zip->addFile($file,$new_filename);      
           }

           $zip->close();
           if(file_exists($zip_name)){
             // push to download the zip
             header('Content-type: application/zip');
             header('Content-Disposition: attachment; filename="'.$zip_name.'"');
             readfile($zip_name);
             // remove zip file is exists in temp path
             unlink($zip_name);
           }
   }
   else
   {
    
    header("Content-Type: image/" . $image_type);
    header("Content-Disposition: filename=\"" . $image_file . "\"");

   }
   readfile($image_path_file);
   exit;
  }
 }

[PHP] 檔案下載--中文檔名
  
$file_display = mb_convert_encoding($attach['name_display'],"BIG-5","UTF-8");

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。