2017-07-03 150 views
0

我正在寫一個腳本,需要存檔文件夾和文件裏面,但我不知道如何做到這一點,如果有一個文件夾內的另一個文件夾。我將通過實例講解,讓規範工作zipArchive如何保存文件夾中的文件夾

Warning: ZipArchive::close(): Read error: Is a directory in /путь до скрипта/public_html/crm/drive/drive.php on line 102

Folder + 
 
     File1 
 
     File2 
 
     And so on (so it works) 
 

 
But does not want to work like that 
 

 
Folder + 
 
       File1 
 
       FOLDER (this does not work)

的問題是如何讓這個如果腳本中所看到的文件夾,還下載瞭如果我看到該文件夾​​內的文件夾也分別下載文件夾中的文件?這裏是我的腳本

if (isset($_POST['createPath'])) {//Check that the button is clicked 
 
     
 
$zip = new ZipArchive(); // Create an archive 
 
$zip_name = time().".zip"; // file name 
 
if ($zip->open($zip_name, ZipArchive::CREATE) !== true) { // open file 
 
    die ("Could not open archive");//If the file does not open 
 
} 
 
$var = $_POST["pathUpload"];// Array of variables that are passed through the form 
 
foreach ($var as $key_var) {// We process the array in a loop 
 
$iterator = new RecursiveDirectoryIterator($key_var);//There is a recursive search of the file system directories 
 
foreach ($iterator as $key => $value) {// We process an array of files 
 
    $path = pathinfo($value);//Check the path or revert the path to the file 
 
    if ($path['basename'] == '.' || $path['basename'] == '..') continue;//Check those files that you download if there are points in the files then download 
 

 
    $zip->addFile(realpath($key), $key);//Add the file to the server 
 

 

 
    
 
} 
 

 
$zip->close();//Close archive 
 
    if (file_exists($zip_name)) { 
 
     // Give the file to download 
 
     header('Content-type: application/zip', 'charset=utf-8'); 
 
     header('Content-Disposition: attachment; filename="'.$zip_name.'"'); 
 
     ob_end_flush();//Buffering since without it nothing will work 
 
     readfile($zip_name); //Read the file 
 

 
     unlink($zip_name);//Delete the variable 
 
    } 
 
} 
 

 
}

回答

0

,因爲你正試圖添加目錄與方法郵編

// this function only for adding files! 
public function addFile ($filename, $localname = null, $start = 0, $length = 0) {} 

方法允許你添加目錄

public function addEmptyDir ($dirname) {} 
錯誤

您遇到的另一個問題是您以錯誤的方式使用目錄迭代器。

// this way only loop on directories in 
$iterator = new RecursiveDirectoryIterator($key_var);//There is a recursive search of the file system directories 

正確的方法是使用在RecursiveDirectoryIteratorRecursiveIteratorIterator - 一起來看看在文檔的選項。

例如:

// the right way to recursive get list of the file system directories and files 
$iterator = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($key_var, RecursiveDirectoryIterator::SKIP_DOTS), // skip . and .. 
      RecursiveIteratorIterator::SELF_FIRST, 
      RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" 
); 

所以要得到它的工作,你的代碼應該是這樣的:

<?php 

if (isset($_POST['createPath'])) {//Check that the button is clicked 

    $zip  = new ZipArchive(); // Create an archive 
    $zip_name = time() . ".zip"; // file name 
    if ($zip->open($zip_name, ZipArchive::CREATE) !== true) { // open file 
     die ("Could not open archive");//If the file does not open 
    } 

    $var = $_POST["pathUpload"];// Array of variables that are passed through the form 
    foreach ($var as $key_var) {// We process the array in a loop 

     // There is a recursive search of the file system directories 
     $iterator = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($key_var, RecursiveDirectoryIterator::SKIP_DOTS), // skip . and .. 
      RecursiveIteratorIterator::SELF_FIRST, 
      RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" 
     ); 

     // all directories and subdir 
     foreach ($iterator as $path => $dir) { 
      if (!$dir->isDir()) && is_file($path) { 
       $zip->addFile(realpath($path)); //Add the file to the server 
      } 
      if ($dir->isDir()) { 
       // do nothing 
      } 
     } 

     $zip->close(); //Close archive 
     if (file_exists($zip_name)) { 
      // Give the file to download 
      header('Content-type: application/zip', 'charset=utf-8'); 
      header('Content-Disposition: attachment; filename="' . $zip_name . '"'); 
      ob_end_flush();//Buffering since without it nothing will work 
      readfile($zip_name); //Read the file 

      unlink($zip_name);//Delete the variable 
     } 
    } 

} 

編碼快樂:)

+0

我很抱歉,但它是可能仍然是一個問題poche這是必要的我介入代碼作爲嚴格文件夾,並沒有錯誤 – Vadim

相關問題