Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get names of files?

Like, we have folder /images/, it has some files inside.

And the script /scripts/listing.php

How can we get names of the all files inside folder /images/, in listing.php?

Thanks.

like image 853
James Avatar asked Nov 16 '10 22:11

James


1 Answers

<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>

See: readdir()

like image 118
Wouter Dorgelo Avatar answered Sep 25 '22 19:09

Wouter Dorgelo