2012-07-18 161 views
20

我試圖從一個文件夾中保存文件另一個。 zip文件夾放置在不同的目錄中。我寫了下面的代碼:創建的.zip文件

archive.php

<?php 
    $zip = new ZipArchive(); 
    $zip->open('example.zip', ZipArchive::CREATE); 
    $srcDir = "/home/sam/uploads/"; 
    $files= scandir($srcDir); 
    //var_dump($files); 
    unset($files[0],$files[1]); 
    foreach ($files as $file) { 
     $zip->addFile("{$file}");  
    } 
    $zip->close(); 
?> 

但遺憾的是我不能創建的.zip文件夾。我錯過了什麼?

+0

你收到了什麼錯誤? – MrGlass 2012-07-18 11:34:15

+0

http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php – dpitkevics 2012-07-18 11:34:35

回答

18
$zip = new ZipArchive(); 

$DelFilePath="first.zip"; 

if(file_exists($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath)) { 

     unlink ($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath); 

} 
if ($zip->open($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) { 
     die ("Could not open archive"); 
} 
    $zip->addFile("file_path","file_name"); 

// close and save archive 

$zip->close(); 

這裏測試項目文件夾的名稱。

您可以根據需要定義路徑。

2

請參見本文下面的例子:

<?php 
    $error = "";  //error holder 
    if(isset($_POST['createpdf'])){ 
     $post = $_POST;  
     $file_folder = "files/"; // folder to load files 
     if(extension_loaded('zip')){ // Checking ZIP extension is available 
      if(isset($post['files']) and count($post['files']) > 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($post['files'] as $file){    
        $zip->addFile($file_folder.$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/>"; 
    } 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Download As Zip</title> 
</head> 
<body> 
<center><h1>Create Zip</h1></center> 
<form name="zips" method="post"> 
<?php if(!empty($error)) { ?> 
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p> 
<?php } ?> 
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;"> 
    <tr> 
    <td width="33" align="center">*</td> 
    <td width="117" align="center">File Type</td> 
    <td width="382">File Name</td> 
    </tr> 
    <tr> 
    <td align="center"><input type="checkbox" name="files[]" value="a.jpg" /></td> 
    <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td> 
    <td>a.jpg</td> 
    </tr> 
    <tr> 
    <td align="center"><input type="checkbox" name="files[]" value="b.jpg" /></td> 
    <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td> 
    <td>b.jpg</td> 
    </tr> 
    <tr> 
    <td align="center"><input type="checkbox" name="files[]" value="c.docx" /></td> 
    <td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td> 
    <td>c.docx</td> 
    </tr> 
    <tr> 
    <td align="center"><input type="checkbox" name="files[]" value="d.pdf" /></td> 
    <td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td> 
    <td>d.pdf</td> 
    </tr> 
    <tr> 
    <td colspan="3" align="center"> 
     <input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Download as ZIP" />&nbsp; 
     <input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" /> 
    </td> 
    </tr> 
</table> 

</form> 
</body> 
</html> 
10

是的,我已經解決了我的問題。

在這裏,我剛更換過代碼

$zip->addFile("{$file}"); 

與代碼

$zip->addFromString(basename($file), file_get_contents($file)); 

,並讓我的工作完成。 :)

+0

這不適用於大文件... – 2018-03-08 09:53:14

3

試試這個代碼:

<?php 
    $zip = new ZipArchive; 
    if ($zip->open(getcwd() . '/test.zip', ZipArchive::CREATE) === TRUE) { 
     $zip->addFile(getcwd() . '/file.txt', 'newname.txt'); 
     $zip->close(); 
     echo 'ok'; 
    } else { 
     echo 'failed'; 
    } 
    ?>