How to get files list from public folder Laravel

Solved! I've used this function and it's work :

// GET PUBLIC FOLDER FILES (NAME)
if ($handle = opendir(public_path('img'))) {

    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo $entry."<br>"; // NAME OF THE FILE
        }
    }
    closedir($handle);
}

Thanks @MyLibary :)


You could do this in one line:

use File;

$files = File::files(public_path());

// If you would like to retrieve a list of 
// all files within a given directory including all sub-directories    
$files = File::allFiles(public_path()); 

For more info, check the documentation.

Edit: The documentation is confusing. It seems, you would need to use the File Facade instead. I will investigate a bit more, but it seems to be working now.

Also, the result will be an array of SplFileInfo objects.


Solution 1 for Laravel


public function index()
{
    $path = public_path('test');
    $files = File::allFiles($path);

    dd($files);
}

Solution 2 for Laravel


public function index()
{
    $path = public_path('test');
    $files = File::files($path);

    dd($files);
}

Solution for PHP


public function index()
{
    $path = public_path('demo');
    $files = scandir($path);
    dd($files);
}