Reputation: 2269
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?
Upvotes: 0
Views: 361
Reputation: 39986
function filename_part($f) {
return pathinfo($f, PATHINFO_FILENAME);
}
$result = array_map("filename_part", scandir($directory));
Upvotes: 4
Reputation: 186762
<?php
foreach (new DirectoryIterator('directory') as $fileInfo) {
if($fileInfo->isDot()) continue;
$regex = '/\.\w+/';
echo preg_replace( $regex, '', $fileInfo->getFilename() ) . '<br>';
}
Upvotes: 3