Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : How to get random image from directory?

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>
like image 632
visulo Avatar asked Dec 15 '16 14:12

visulo


People also ask

How to get random Database records from Laravel?

We use inRandomOrder () method to ger random records in laravel. This section will instruct you on how to use the inRandomOrder () method to get random database records from laravel. Notice that it is possible to use the Laravel inRandomOrder method to randomly sort the query data. If, for instance, you want to get random DB posts in Laravel.

How to get all files from specific directory in Laravel?

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.

Why Laravel introduce storage folder file upload?

All most developer and client want to store images or files secure on his server. so anyone can not access directly using url. that's the reason laravel introduce storage folder file upload. Almost using storage folder for uploading files and images on his laravel app.

How can I generate a random image from a table image?

Assuming you have an associated Eloquent model for your table, a simple option could be to define a method on your RandomImagemodel (could probably just be called Image). This way, you can just use your Imagemodel to generate and pass a random image to any view you would like.


2 Answers

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

$files = Storage::allFiles($directory);
$randomFile = $files[rand(0, count($files) - 1)];
like image 65
Alexey Mezenin Avatar answered Sep 23 '22 09:09

Alexey Mezenin


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.

like image 40
The Alpha Avatar answered Sep 23 '22 09:09

The Alpha