2016-11-14 191 views
0

我有一個名爲「檢查」的文件夾,其中有一些名爲「位置1」,「位置2」,「位置3」等子文件夾, 10-20個PNG圖像。PHP - 閱讀文件夾,所有子文件夾和子文件夾中的文件

我想要做的是讀取「檢查」文件夾的目錄,然後讀取每個文件夾中的所有圖像文件,然後將它們作爲我的index.php網站上的圖庫返回。問題是我沒有收到錯誤消息,但腳本不返回任何內容。我相信問題在於爲每個子文件夾生成$ files變量,因爲我部署了相同的腳本來讀取另一個站點上的文件夾內容。

也許有人可以指出我正確的方向嗎?

<?php 
$dir = './inspections/'; 
if ($handle = opendir($dir)) 
{ 
    $blacklist = array('.', '..', 'default', 'default.php', 'desc.txt'); 
    while (false !== ($folder = readdir($handle))) 
    { 
     if (!in_array($folder, $blacklist)) 
     { 
      if (file_exists($dir . $folder . '/desc.txt')) 
       { 
        while (false !== ($file = readdir($handle))) 
        { 
         if (!in_array($file, $blacklist)) 
         { 
          $chain = file_get_contents($dir . $folder . '/chain.txt'); 
          $website = file_get_contents($dir . $folder . '/website.txt'); 
          $location = file_get_contents($dir . $folder . '/location.txt'); 
          $desc = file_get_contents($dir . $folder . '/desc.txt', NULL, NULL, 0, 250) . '...'; 
          echo " 
           <!-- Post --> 
           <div class=\"post\"> 
            <div class=\"user-block\"> 
            <img class=\"img-circle img-bordered-sm\" src=\"../dist/img/logo/logo_".$chain."\" alt=\"\"> 
             <span class=\"username\"> 
              <a href=\"".$website."\" target=\"_blank\">".$folder."</a> 
             </span> 
            <span class=\"description\"><i class=\"fa fa-map-pin\"></i> ".$location." - Posted on ". $date . "</span> 
            </div> 
            <!-- /.user-block --> 
            <p>".$desc."</p> 
            <div class=\"lightBoxGallery\"> 
            <a href=\"".$dir . $folder . "/".$file."\" title=\"".$file."\" data-gallery=\"\"><img src=\"".$dir . $folder . "/".$file."\" style=\"height:100px; width:100px;\"></a> 
            </div> 
          "; 
         } 
        } 
       } 
     } 
    } 
    closedir($handle); 
} 
?> 

編輯: 以下@JazZ的建議,我已經調整了代碼,現在工作得很好,但是,假設不希望顯示調整大小的圖片本身是一個,而是存儲在一個子文件夾,而縮略圖(例如, ./location1/thumbs/),我會怎麼做呢?

<?php     
$dir = './inspections/'; 
if ($handle = opendir($dir)) { 
    $blacklist = array('.', '..', 'default', 'default.php', 'desc.txt'); 
    while (false !== ($folder = readdir($handle))) { 
     if (!in_array($folder, $blacklist)) { 

        echo " 
         <!-- Post --> 
         <div class=\"post\"> 
          <div class=\"user-block\"> 
          <img class=\"img-circle img-bordered-sm\" src=\"../dist/img/logo/gallery_icon_".$chain.".jpg\" alt=\"\"> 
           <span class=\"username\"> 
            <a href=\"".$website."\" target=\"_blank\">".$hotel_name."</a>".$status." 
           </span> 
          <span class=\"description\"><i class=\"fa fa-map-pin\"></i> ".$location." - Posted on ".date('jS F, Y - H:m', strtotime($posted_on))."</span> 
          </div> 
          <!-- /.user-block --> 

          <p>".$desc."</p> 
          <div class=\"lightBoxGallery\"> 
        "; 

        foreach (glob($dir . $folder . "/*.jpg") as $filename) { 
         echo " 
          <a href=\"".$filename."\" title=\"\" data-gallery=\"\"><img src=\"".$filename."\" style=\"height:100px; width:100px;\"></a>"; 
        } 
        echo "</div> 
         </div> 
         <!-- /. POST --> 

         "; 

     } 
    } 
    closedir($handle); 
} 
?> 

回答

1

我覺得你的問題都來源於這裏:

while (false !== ($file = readdir($handle))) // it reads again the same directory as it did in the first while loop 

嘗試

if ($sub_handle = opendir($dir . $folder)) { 
    while (false !== ($file = readdir($sub_handle))) { 
     ... 
    } 
    closedir($sub_handle); 
} 

來代替也行,你的情況,我會用PHP glob() function

見一個適用於您的案例的實例:

$dir = './inspections/'; 
if ($handle = opendir($dir)) { 
    $blacklist = array('.', '..', 'default', 'default.php', 'desc.txt'); 
    while (false !== ($folder = readdir($handle))) { 
     if (!in_array($folder, $blacklist)) { 
      foreach (glob($dir . $folder . "/*.png") as $filename) { 
       echo "$filename was found !"; 
       echo "\r\n"; 
      } 
     } 
    } 
    closedir($handle); 
} 

輸出:

./inspections/location_4/img_1.png was found ! 
./inspections/location_4/img_2.png was found ! 
./inspections/location_4/img_3.png was found ! 
./inspections/location_4/img_4.png was found ! 
./inspections/location_4/img_5.png was found ! 
./inspections/location_4/img_6.png was found ! 
./inspections/location_3/img_1.png was found ! 
./inspections/location_3/img_2.png was found ! 
./inspections/location_3/img_3.png was found ! 
./inspections/location_3/img_4.png was found ! 
./inspections/location_3/img_5.png was found ! 
./inspections/location_3/img_6.png was found ! 
./inspections/location_2/img_1.png was found ! 
./inspections/location_2/img_2.png was found ! 
./inspections/location_2/img_3.png was found ! 
./inspections/location_2/img_4.png was found ! 
./inspections/location_2/img_5.png was found ! 
./inspections/location_2/img_6.png was found ! 
./inspections/location_1/img_1.png was found ! 
./inspections/location_1/img_2.png was found ! 
./inspections/location_1/img_3.png was found ! 
./inspections/location_1/img_4.png was found ! 
./inspections/location_1/img_5.png was found ! 
./inspections/location_1/img_6.png was found ! 

編輯

要在/檢查/ LOCATION1 /大拇指/目錄循環,這會工作:

foreach (glob($dir . $folder . "/thumbs/*.png") as $filename) { 
    echo "$filename was found !"; 
    echo "\r\n"; 
} 

RE-EDIT

glob的要與​​3210功能的多個文件夾,你的代碼應該是這樣的:

foreach (glob($dir.$folder."{/thumbs/*.png,/*.png}", GLOB_BRACE) as $filename) { 
    echo "$filename was found !"; 
    echo "\r\n"; 
} 
+0

完美無缺地工作,謝謝。我建議的唯一的事情就是調整腳本以接受各種圖像,而不僅限於* .png(儘管這只是我需要和要求的,只是一個建議)。非常感謝! – Armitage2k

+0

假設每個位置文件夾中都有一個'/ thumbs /'文件夾,那麼如何將該文件夾包含到$ filename數組中?我想爲每個找到的$ filename顯示$ thumb,但這意味着該數組需要「foreach循環」的多個變量,這可以完成嗎? – Armitage2k

+0

您能否以新的要求更新您的問題。今天下午會看。 – JazZ

0

也許下面的函數幫助。它也有些評論。它遞歸掃描一個目錄(在這種情況下,從每個目錄/子目錄中提取圖像文件)。

<?php 

    $rootPath = './inspections/'; 
    $regex  = "#(\.png$)|(\.jpg$)|(\.jpeg$)|(\.tiff$)|(\.gif$)#"; 


    /** 
    * @param string $directory => DIRECTORY TO SCAN 
    * @param string $regex  => REGULAR EXPRESSION TO BE USED IN MATCHING FILE-NAMES 
    * @param string $get   => WHAT DO YOU WANT TO GET? 'dir'= DIRECTORIES, 'file'= FILES, 'both'=BOTH FILES+DIRECTORIES 
    * @param bool $useFullPath => DO YOU WISH TO RETURN THE FULL PATH TO THE FOLDERS/FILES OR JUST THEIR BASE-NAMES? 
    * @param array $dirs   => LEAVE AS IS: USED DURING RECURSIVE TRIPS 
    * @return array 
    */ 
    function scanDirRecursive($directory, $regex=null, $get="file", $useFullPath=false, &$dirs=[], &$files=[]) { 
     $iterator    = new DirectoryIterator ($directory); 
     foreach($iterator as $info) { 
      $fileDirName  = $info->getFilename(); 

      if ($info->isFile() && !preg_match("#^\..*?#", $fileDirName)) { 
       if($get == 'file' || $get == 'both'){ 
        if($regex) { 
         if(preg_match($regex, $fileDirName)) { 
          if ($useFullPath) { 
           $files[] = $directory . DIRECTORY_SEPARATOR . $fileDirName; 
          } 
          else { 
           $files[] = $fileDirName; 
          } 
         } 
        }else{ 
         if($useFullPath){ 
          $files[] = $directory . DIRECTORY_SEPARATOR . $fileDirName; 
         }else{ 
          $files[] = $fileDirName; 
         } 
        } 
       } 
      }else if ($info->isDir() && !$info->isDot()) { 
       $fullPathName = $directory . DIRECTORY_SEPARATOR . $fileDirName; 
       if($get == 'dir' || $get == 'both') { 
        $dirs[]  = ($useFullPath) ? $fullPathName : $fileDirName; 
       } 
       scanDirRecursive($fullPathName, $regex, $get, $useFullPath, $dirs, $files); 
      } 
     } 

     if($get == 'dir') { 
      return $dirs; 
     }else if($get == 'file'){ 
      return $files; 
     } 
     return ['dirs' => $dirs, 'files' => $files]; 
    } 

    $images = scanDirRecursive($rootPath, $regex, 'file', true); 
    var_dump($images); 
相關問題