2012-12-31 48 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类,这是(大大)加快。

相关问题