2012-12-31 73 views
0

我有一個「路徑/到/文件夾」。 什麼是最好的方法來獲得沒有文件名的文件數組?php如何獲得文件夾中沒有擴展名的文件列表

我可以使用$files = scandir("path/to/folder/", 1); 但我必須刪除擴展名和「。」來自該陣列。 有沒有更好的方法來獲取文件數組?

剛剛加上: P.S.我需要「路徑/到/文件夾」中的文件沒有該文件夾中的子文件夾。 因此,如果該文件夾中的文件是"ab.php, cd.php, 123.php, hello.php"

那麼結果是:$files =array('ab', 'cd', '123', 'hello');

+0

只得到任何你想要的方式排列文件,然後使用'基名()'來獲得沒有擴展名的文件名。 – kennypu

+0

http://php.net/manual/en/refs.fileprocess.file.php –

回答

1

你可以嘗試擴大FilesystemIterator所有你需要的是:

echo new NoExtentionIterator(__DIR__); 

類用於

class NoExtentionIterator extends FilesystemIterator { 
    public function current() { 
     return pathinfo(parent::current(), PATHINFO_FILENAME); 
    } 
    public function accept() { 
     return parent::current()->isfile(); 
    } 
    public function __toString() { 
     return implode("\n", iterator_to_array($this)); 
    } 
} 
+0

通常情況下,'FilterIterator'將被擴展用於這樣的任務。 – salathe

1
$files = glob('path/to/files/*.*'); 
foreach($files as $file) { 
    $file = pathinfo($file); 
    echo $file['filename']; 
} 

或者,使用DirectoryIterator類,這是(大大)加快。

相關問題