Getting a list of all files in a directory

2019-08-25 04:47发布

I want to do following 2 things:

1) Retrieve the list of all files in a directory.

2) and then remove their extensions.

e.g.: If I get a list of files as A.png, B.png, C.jpeg, D.txt, I want to get A,B,C,D.

How do I do that in php?

标签: php string file
3条回答
不美不萌又怎样
2楼-- · 2019-08-25 05:30
function filename_part($f) {
    return pathinfo($f, PATHINFO_FILENAME);
}

$result = array_map("filename_part", scandir($directory));
查看更多
甜甜的少女心
3楼-- · 2019-08-25 05:38
<?php
foreach (new DirectoryIterator('directory') as $fileInfo) {
        if($fileInfo->isDot()) continue;
    $regex = '/\.\w+/';
    echo preg_replace( $regex, '', $fileInfo->getFilename() ) . '<br>';
}
查看更多
祖国的老花朵
4楼-- · 2019-08-25 05:48

Check out the glob function for directory listing, and use this for removing the extension:

substr($filename, 0,strrpos($filename,'.')
查看更多
登录 后发表回答