2014-11-21 79 views
0

我有一個腳本和一個函數在其中獲取該文件夾中的每個文件。如何編輯此代碼以使其僅獲取.png文件?PhP搜索目錄通過文件擴展名

public function GetPreviews($category) 
    { 
     $directories = glob("assets/preview/$category/*"); 
     $directory = array(); 
     foreach($directories as $directories) 
     { 
      $directory[] = str_replace("assets/preview/$category/", "", $directories); 
     } 
     return $directory;  
    } 
+1

你看了手冊http://php.net/manual/en/function.glob.php – 2014-11-21 14:34:54

回答

0

由於您使用​​3210 - 剛剛過去的*

之後添加.png像這樣:

public function GetPngs($category) 
    { 
     $directories = glob("assets/preview/$category/*.png"); 
     $directory = array(); 
     foreach($directories as $directories) 
     { 
      $directory[] = str_replace("assets/preview/$category/", "", $directories); 
     } 
     return $directory;  
    } 
0

你缺少你的代碼 'png格式' 只是改變

$directories = glob("assets/preview/$category/*"); 

$directories = glob("assets/preview/$category/*.png"); 
1

將.png添加到glob(「assets/preview/$ category/*。png」);

public function GetPreviews($category) 
    { 
     $directories = glob("assets/preview/$category/*.png"); 
     $directory = array(); 
     foreach($directories as $directories) 
     { 
      $directory[] = str_replace("assets/preview/$category/", "", $directories); 
     } 
     return $directory;  
    }