Laravel 8 get all Files in Directory Example

  1431 views   9 months ago Laravel

You may want to get images from server and process them just like from images from form data. This is useful when you want to manipulate images when server using low memory. Handling files in Laravel is more easy.

In this article, we will share you how you can get images from public or storage folder in Laravel. Suppose you have a user profile picture of specific size and you want to create images of multiple sizes. Laravel's File class used to get all files from specific directory in array format.

There are two ways you can get files in Laravel. If you only want to get files from specific directory, then you can use files() method from File facade class. Let's see this in example so you can get better idea.

/**
 * create multiple thumbnail of profile
 *
 * @return void
 */
public function cron()
{
    $path = public_path('images/profiles/');
    $files = \File::files($path);
    
    dd($files);
}

array:2 [▼
    0 => Symfony\Component\Finder\SplFileInfo {#290 ▶}
    1 => Symfony\Component\Finder\SplFileInfo {#289 ▶}
]

In the second way, if you want to get all files including files from subdirectory also, then you can use allFiles() method instead of files() method. Here is the example:

/**
 * create multiple thumbnail of profile
 *
 * @return void
 */
public function cron()
{
    $path = public_path('images/profiles/');
    $files = \File::allFiles($path);

    foreach ($files as $key => $file) {
        // loop through all images...
    }    
}

While we have discussed way to get Files from directory using Laravel classes, we can also do this using PHP. PHP readdir() function is used to get file details from the directory.

/**
 * create multiple thumbnail of profile
 *
 * @return void
 */
public function cron()
{
    $path = opendir(public_path('images')); // open directory to read files

        while (false !== ($entry = readdir($path))) { // loop through all files
            if ($entry != "." && $entry != "..") { // remove back entry from list
                echo "$entry <br>\n";
            }
        }

        closedir($path);
}

PHP scandir() function also do same as readdir() function. It doesn't require to call opendir() function or closedir() function.

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function index()
{
    $entry = scandir(public_path('images'));

    foreach ($entry as $key => $value) {
        if ($value != "." && $value != "..") {
            echo "$value <br>\n";
        }
    }
}

So you can all these ways to get directory files list. If you are using Laravel and want to get simple way, it is better to use File facade of Laravel.

I hope you liked this article and help in your way.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]