Asked  7 Months ago    Answers:  5   Viewed   25 times

How to get the file names inside a directory using PHP?

I couldn't find the relevant command using Google, so I hope that this question will help those who are asking along the similar lines.



There's a lot of ways. The older way is scandir but DirectoryIterator is probably the best way.

There's also readdir (to be used with opendir) and glob.

Here are some examples on how to use each one to print all the files in the current directory:

DirectoryIterator usage: (recommended)

foreach (new DirectoryIterator('.') as $file) {
    if($file->isDot()) continue;
    print $file->getFilename() . '<br>';

scandir usage:

$files = scandir('.');
foreach($files as $file) {
    if($file == '.' || $file == '..') continue;
    print $file . '<br>';

opendir and readdir usage:

if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if($file == '.' || $file == '..') continue;
        print $file . '<br>';

glob usage:

foreach (glob("*") as $file) {
    if($file == '.' || $file == '..') continue;
    print $file . '<br>';

As mentioned in the comments, glob is nice because the asterisk I used there can actually be used to do matches on the files, so glob('*.txt') would get you all the text files in the folder and glob('image_*') would get you all files that start with image_

Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago
function dir_contents_recursive($dir) {
    // open handler for the directory
    $iter = new DirectoryIterator($dir);

    foreach( $iter as $item ) {
        // make sure you don't try to access the current dir or the parent
        if ($item != '.' && $item != '..') {
            if( $item->isDir() ) {
                // call the function on the folder
            } else {
                // print files
                echo $dir . "/" .$item->getFilename() . "<br>";
Saturday, June 19, 2021
answered 5 Months ago

psutil is a cross-platform package which can identify partition types:

>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'),
 sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext4', opts='rw')]

Warning: On linux, the fstype may be reported as ext4 or ntfs, but on Windows, the fstype is limited to "removable", "fixed", "remote", "cdrom", "unmounted" or "ramdisk".

Sunday, August 15, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :