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 :)

Recommended Answers

All 2 Replies

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;

Thank you very much cereal !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.