2016-05-13 88 views
1

我有一個腳本:PHP - 遍歷文件夾,打開每個文件的docx和替換字符串

  • 經過每個文件夾和我的「./sample/」目錄的子目錄
  • 打開每個.DOCX文件
  • 代替一個字符串,如 「## PROPERTY ##」 與我的$ _ POST [ '財產']變量
  • 呼嘯而過的文件夾內容
  • 啓動一個下載

現在,單獨運行部分代碼,它可以完成所需的任務。但是,將它們放在一起時,它會在掃描docx文件的子文件夾時死亡。

我的文件夾結構是這樣的:

./sample/

  • /IT/it1.docx
  • /F & B/fb1.docx
  • /FO/FO1。 DOCX
  • sample1.docx

中暴露出的問題eems在level 1文件夾的is_dir($ dir)部分中發生。 任何想法可能導致這種情況?

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { 

    // form variables 
    $holidex = strtoupper($_POST['holidex']); 
    $property = $_POST['property']; 
    $brand = $_POST['brand']; 
    $division = $_POST['division']; 
    $language = $_POST['language']; 
    $date_issued = $_POST['date_issued']; 
    $approved_by = $_POST['approved_by']; 

    // script variables 
    //$dir = './Sample_SOP_-_'.$brand.'_('.$language.')'; //dir to scan like ./sample/ 
    $dir = './sample/';          //dir to scan like ./sample/ 
    $archive = date("Y-m-d H-i-s");       //UNIQUE name of zip file to create and download 
    $zipfile = "./".$holidex." - ".$archive.".zip";   //path to zip file download 
    $temp = "./temp/";          //directory to temp folder 

    // string replacements 
    $find = "##PROPERTY##";         // find and replace property information 
    $replace = $_POST['property']; 

    $find2 = "##BRAND##";         // find and replace brand information 
    $replace2 = $_POST['brand']; 

    $find3 = "##DATE##";         // find and replace effective date 
    $replace3 = $_POST['date_issued']; 

    $find4 = "##APPROVED_BY##";        // find and replace approved by 
    $replace4 = $_POST['approved_by']; 

    //read dir 
    $files = scandir($dir, 1); 

    //create new archive name 
    $zip_download = new ZipArchive(); 
    $zip_download->open("$zipfile", ZipArchive::CREATE); 

    foreach($files as $file) { 

     //docx 
     $ext1 = ".docx"; 
     $checkextension = explode($ext1, $file); 
     if (count($checkextension) > 1) { 
     $zip = new ZipArchive; 
     $zip->open("$file"); 

     $word = $zip->getFromName('word/document.xml'); 
     $word2 = str_replace($find, $replace, $word); 
     $word2 = str_replace($find2, $replace2, $word); 
     $word2 = str_replace($find3, $replace3, $word); 
     $word2 = str_replace($find4, $replace4, $word); 

     $zip->addFromString("word/document.xml", $word2); 
     $zip->close(); 
     } else { 
      die("Error - There are no files the directory.."); 
      } 

     //folders level 1 
     if (is_dir($file)) { 
     $sub = $file . '/'; 
     $subfiles = scandir($sub, 1); 
     if ($subfiles > 1) { 
      if ($sub == "../" || $sub == "./") { 
      } 
      else { 
      foreach($subfiles as $subfile) { 

       //docx 
       $ext1 = ".docx"; 
       $checkextensionsub = explode($ext1, $subfile); 
       $subsubfile = $sub . $subfile; 
       if (count($checkextensionsub) > 1) { 
       $zipsub = new ZipArchive; 
       $zipsub->open("$subsubfile"); 

       $wordsub = $zipsub->getFromName('word/document.xml'); 
       $word2sub = str_replace($find, $replace, $wordsub); 
       $word2sub = str_replace($find2, $replace2, $wordsub); 
       $word2sub = str_replace($find3, $replace3, $wordsub); 
       $word2sub = str_replace($find4, $replace4, $wordsub); 

       $zipsub->addFromString("word/document.xml", $word2sub); 
       $zipsub->close(); 
       } 

       //folders level 2 
       $sub2 = $sub . $subfile; 
       if (is_dir($sub2)) { 
       $subfiles2 = scandir($sub2, 1); 
       if ($subfiles2 > 1) { 
        if ($sub2 == $sub.".." || $sub2 == $sub.".") { 
        } 
        else { 
        foreach($subfiles2 as $subfile2) { 
         //docx 
         $ext1 = ".docx"; 
         $checkextensionsub2 = explode($ext1, $subfile2); 
         $subsubfile2 = $sub2 . '/' . $subfile2; 
         if (count($checkextensionsub2) > 1) { 
         $zipsub2 = new ZipArchive; 
         $zipsub2->open("$subsubfile2"); 

         $wordsub2 = $zipsub2->getFromName('word/document.xml'); 
         $word2sub2 = str_replace($find, $replace, $wordsub2); 
         $word2sub2 = str_replace($find2, $replace2, $wordsub2); 
         $word2sub2 = str_replace($find3, $replace3, $wordsub2); 
         $word2sub2 = str_replace($find4, $replace4, $wordsub2); 

         $zipsub2->addFromString("word/document.xml", $word2sub2); 
         $zipsub2->close(); 
         } 

         //more directories when needed 
         //****replicate code here**** 

         //add files to archive 
         $zip_download->addFile($subsubfile2, $subsubfile2); 
        } 
        } 
       } 
       } 

       //add files to archive 
       $zip_download->addFile($subsubfile, $subsubfile); 
      } 
      } 
     } 
     } else { 
       die ("Error - No files in the directory"); 
       } 
     } 

     //add files to archive 
     $zip_download->addFile($file, $file); 

    } 

    $zip_download->close(); 

    //download zip 
    if (file_exists($zipfile) && is_readable($zipfile)) { 
     header('Content-Type: application/octet-stream'); 
     header('Content-Length: '.filesize($zipfile)); 
     header('Content-Disposition: attachment; filename="'.basename($zipfile).'";'); 
     header('Content-Transfer-Encoding: binary'); 
     $file_download = @ fopen($zipfile, 'rb'); 
     if ($file_download) { 
     fpassthru($file_download); 
     exit; 
     } 

     echo ("ZIP generated successfully, download is starting..."); 

    } else { 
      echo ("Error creating the ZIP archive!"); 
      } 
} 
?> 

回答

0

我想這是因爲SCANDIR()將在arrayand後產生類似

`Array 
    (
     ... 
     [9] => . 
     [10] => ..` 

當你檢查這些,如果他們的文件夾可能出現錯誤和死亡。

相關問題