2010-07-02 68 views
0

嗨frnds任何人都可以打電話給我如何使用PHP創建一個Zip文件? 其實我有10個文件的框,如果我選擇多個複選框,文件應該得到zip/rar,我可以保存在一些路徑..如何創建rar/zip文件和下載

請任何人都可以在此解決方案的幫助,因爲我我新的PHP

回答

2
$zip_name = 'path/to/final.zip'; //the real path of your final zip file on your system 
$zip = new ZipArchive; 
$zip->open($zip_name, ZIPARCHIVE::CREATE); 
foreach($files as $file) 
{ 
    $zip->addFile($file); 
} 
$zip->close(); 

header('Content-type: application/zip'); 
header('Content-disposition: filename="' . $zip_name . '"'); 
header("Content-length: " . filesize($zip_name)); 
readfile($zip_name); 
exit(); 
+0

路徑/到/ final.zip這是這條道路?我想在我的系統中安裝winzip?實際上我正在使用winrar .. – user346797 2010-07-02 09:08:38

+0

編輯了我關於路徑的答案。不,你不需要安裝任何第三方軟件。 ZipArchive是PHP內置的Zip擴展。你可以在PHP 5.2.0+ – fabrik 2010-07-02 09:13:47

+0

開箱即用的情況下使用它,但是無論我在複選框中選擇哪些文件,文件不會進入zipfolder..wat是問題所在? – user346797 2010-07-02 09:27:23

1
// This example creates a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt. 
$zip = new ZipArchive; 
$res = $zip->open('test.zip', ZipArchive::CREATE); 
if ($res === TRUE) { 
    $zip->addFile('/path/to/index.txt', 'newname.txt'); 
    $zip->close(); 
    echo 'ok'; 
} else { 
    echo 'failed'; 
} 
+0

wht is that test.zip 也dint get('/path/to/index.txt','newname.txt'); 實際上我想爲word文檔創建zip – user346797 2010-07-02 09:37:04

-1
//include your connection file 
//$file_names[] get your files array 
//$error = "";  //error holder 
//$file_path='set your image path' folder to load files 
if(extension_loaded('zip')) 
{ // Checking ZIP extension is available 
    if(count($file_names) > 0) 
    { 
     // Checking files are selected 
     $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<br/>"; 
     } 
     foreach($file_names as $file){ 
      $zip->addFile($file_path.$file);   // Adding files into zip 
     } 
     $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 .= "* Please select file to zip <br/>"; 
    } 
} 
else 
{ 
    $error .= "* You dont have ZIP extension<br/>"; 
} 
?> 
相關問題