Asked  7 Months ago    Answers:  5   Viewed   51 times

Possible Duplicate:
Get the hierarchy of a directory with PHP
Getting the names of all files in a directory with PHP

I have seen functions to list all file in a directory but how can I list all the files in sub-directories too, so it returns an array like?

$files = files("foldername");

So $files is something similar to

array("file.jpg", "blah.word", "name.fileext")


foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename)
    // filter out "." and ".."
    if ($filename->isDir()) continue;

    echo "$filenamen";

PHP documentation:

  • RecursiveDirectoryIterator
  • RecursiveIteratorIterator
Wednesday, March 31, 2021
answered 7 Months ago
$d = dir(".");

echo "<ul>";

while (false !== ($entry = $d->read()))
    if (is_dir($entry) && ($entry != '.') && ($entry != '..'))
        echo "<li><a href='{$entry}'>{$entry}</a></li>";

echo "</ul>";

Saturday, May 29, 2021
answered 5 Months ago

Have you tried...

$mail->AddAttachment($full_path_to_file, "report.xls");
Saturday, May 29, 2021
answered 5 Months ago

Check this out : readdir()

This bit of code should list all entries in a certain directory:

if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entryn";


Edit: miah's solution is much more elegant than mine, you should use his solution instead.

Sunday, June 6, 2021
answered 5 Months ago

Not an answer to your question, but if you don't need to do extensive operations on filesystem level (like searching, sorting...), there is a nice cross-platform workaround for the issue outlined in this SO question: URLEncode()ing file names.


gets turned into


which should be safe to use in any filesystem and is able to map any UTF-8 character.

I find this much preferable to trying to "natively" deal with the host OS's capabilities, which is guaranteed to be complicated and error-prone (in addition to operating system differences, I'm sure the various filesystem formats - FAT16, FAT32, NTFS, extFS versions 1/2/3.... bring their own set of rules to be aware of.)

Monday, August 23, 2021
answered 2 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 :