Laravel:如何从目录中获取随机图像? [英] Laravel : How to get random image from directory?
本文介绍了Laravel:如何从目录中获取随机图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含子目录的目录,每个子目录中都有图像.我想随机显示图像.
在我的php代码下面运行良好,但是在Laravel中不起作用,问题出在opendir()
和readdir()
.
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()
.
查看刀片
<?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>
推荐答案
在Laravel中,您需要使用 Storage
与文件系统一起使用.
In Laravel you need to use Storage
to work with filesystem.
$files = Storage::allFiles($directory);
$randomFile = $files[rand(0, count($files) - 1)];
这篇关于Laravel:如何从目录中获取随机图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文