php - 拉维尔 : How to get random image from directory?

标签 php image laravel random

我有一个包含子目录的目录,每个子目录中都有图像。我想随机显示图像。 在我的 php 代码下方运行良好,但在 Laravel 中不起作用,问题出在 opendir()readdir() 上。

查看 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>

最佳答案

在 Laravel 中你需要使用 Storage使用文件系统。

$files = Storage::allFiles($directory);
$randomFile = $files[rand(0, count($files) - 1)];

关于php - 拉维尔 : How to get random image from directory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41166308/

相关文章:

php - PHP 内存泄漏工具?使用灯

php - 如何从 Woocommerce 电子邮件中删除价格

html - 在服务器上创建许多图像或在浏览器中调整大小?

php - fatal error : Class 'Illuminate\Foundation\Application' not found

php - 我怎样才能为多表结果做 Laravel 分页?

php - 在 Laravel 中安排多个 cron

php - Laravel vue 路由

php - 在wordpress的主题functions.php中获取全局$post

algorithm - 识别规则网格中的扭曲

windows - 使用GDI显示图像的红色 channel