user177785
user177785

Reputation: 2269

Getting a list of all files in a directory

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

Answers (3)

Scott Evernden
Scott Evernden

Reputation: 39986

function filename_part($f) {
    return pathinfo($f, PATHINFO_FILENAME);
}

$result = array_map("filename_part", scandir($directory));

Upvotes: 4

meder omuraliev
meder omuraliev

Reputation: 186762

<?php
foreach (new DirectoryIterator('directory') as $fileInfo) {
        if($fileInfo->isDot()) continue;
    $regex = '/\.\w+/';
    echo preg_replace( $regex, '', $fileInfo->getFilename() ) . '<br>';
}

Upvotes: 3

Zed
Zed

Reputation: 57678

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

substr($filename, 0,strrpos($filename,'.')

Upvotes: 1

Related Questions