6

I have a directory containing the sub-directory, in each sub-directory there are images. I want to display the images randomly. Below my code in php that works well, but it does not work in Laravel, problem is with opendir() and readdir().

view blade

<?php
$folder = opendir('images/');

$i = 0;
while(false !=($file = readdir($folder))){
if($file != "." && $file != ".."){
    $images[$i]= $file;
    $i++;
    }
}
$random_img=rand(0,count($images)-1);
?> 

<div>
<?php
echo '<img src="images/'.$images[$random_img].'" alt="" />';
?>
</div>
jophab
  • 5,356
  • 14
  • 41
  • 60
visulo
  • 373
  • 2
  • 9
  • 23
  • Check http://stackoverflow.com/questions/7121479/listing-all-the-folders-subfolders-and-files-in-a-directory-using-php , you may get the list of files inside directory and sub-directories and then may put the random order. – Dev Dec 15 '16 at 14:18
  • Answered here https://stackoverflow.com/questions/41166308/laravel-how-to-get-random-image-from-directory – Ray Zion Jun 07 '20 at 14:48

6 Answers6

5

In Laravel you need to use Storage to work with filesystem.

$files = Storage::allFiles($directory);
$randomFile = $files[rand(0, count($files) - 1)];
Rob
  • 6,758
  • 4
  • 46
  • 51
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
2

Actually, you can use Laravel Filesystem but to make it completely working, you've to setup the configuration. For example, the following code will not work:

$dir = 'images'; // public/images
$files = \Storage::allFiles($dir);

Because, the Filesystem uses configuration from config/filesystems.php where you may have something like this:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
    // ...
]

By default, Laravel uses local disk and it points to a different path. So, to make it working, you've to setup your disk in the config, for example, add the following entry into the array:

'web' => [
    'driver' => 'local',
    'root' => base_path('public'),
],

Then, you may use something like this:

$dir = 'images'; // public/images
if($files = \Storage::disk('web')->allFiles('images')) {
    $path = $files[array_rand($files)];
}

To use this $path in your view use <img src="{{asset($path)}}">. Check more here.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
1

You can use allFiles method of Laravel to get all the files and get one of the images using your random logic.

File::allFiles($directory)
Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
0

Here is the way to do this. I am with have now laravel 5.7, but should work for older versions too.

$files = Storage::files('path/to/directory');
$randomFile = array_random($files);
GabMic
  • 1,422
  • 1
  • 20
  • 37
0

You can use a Laravel method where you can return 1 or more random elements;

$all = Storage::allFiles($directory);

$rand = Arr::random($all, 1);

Dont forget the facade:

use Illuminate\Support\Arr;
Mihai
  • 26,325
  • 7
  • 66
  • 81
0

Try this:

$files = Storage::allFiles($directory); 

$randomFile = $files[array_rand($files)];
Jonathan
  • 1,955
  • 5
  • 30
  • 50
Amin Karimi
  • 73
  • 1
  • 9