I have a "path/to/folder". What's the best method to get an array of files without file names?

I can use $files = scandir("path/to/folder/", 1); but then I have to remove extensions and "." from that array. Is there any better way to get an array of files?

Just added: P.S. I need the files in the "path/to/folder" without sub-folders in that folder. So, if the files in that folder are "ab.php, cd.php, 123.php, hello.php",

then the result is: $files =array('ab', 'cd', '123', 'hello');

有帮助吗?

解决方案 2

You can try extending FilesystemIterator all you need would be :

echo new NoExtentionIterator(__DIR__);

Class Used

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));
    }
}

其他提示

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

Or, use the DirectoryIterator class which is (much) faster.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top