0

How do i return an array with the file names from folder? I have this till now but doesn't work.

function showFiles($path) {
    $folder = "gallery/$path";
    $i = 0;
    if (is_dir($folder)) {
        if ($handle = opendir($folder)) {
            while (($file = readdir($handle)) != FALSE) {
                if ($file === "." || $file === ".." || $file === "index.html") { continue; }
                if (!in_array($file, array('.', '..')) && !is_dir($folder.$file))
                    $arrayName = array($file);
                    foreach ($arrayName as $fileArr) {
                    $files[] = $fileArr;
                    print_r($files);
                }
            }
            return $files;
            closedir($handle);
        }
    }
}

Any help will be appreciated :)

Edited by Stefan_1

2
Contributors
2
Replies
30
Views
10 Months
Discussion Span
Last Post by Stefan_1
1

You could use the RecursiveDirectoryIterator() something like in this comment:

More precisely like this:

<?php

$path = dirname(__DIR__);

$dir_iterator = new RecursiveDirectoryIterator($path
                     , FilesystemIterator::SKIP_DOTS);

$iterator     = new RecursiveIteratorIterator($dir_iterator
                    , RecursiveIteratorIterator::LEAVES_ONLY
                    , RecursiveIteratorIterator::CATCH_GET_CHILD);

foreach($iterator as $file)
    if(TRUE === $file->isReadable())
        echo $file . PHP_EOL;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.