Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:如何从目录中获取随机图像?_Php_Image_Laravel_Random - Fatal编程技术网

Php Laravel:如何从目录中获取随机图像?

Php Laravel:如何从目录中获取随机图像?,php,image,laravel,random,Php,Image,Laravel,Random,我有一个包含子目录的目录,在每个子目录中都有图像。我想随机显示图像。 下面是我用php编写的代码,它运行良好,但在Laravel中不起作用,问题在于opendir()和readdir() 视图刀片 <?php $folder = opendir('images/'); $i = 0; while(false !=($file = readdir($folder))){ if($file != "." && $file != ".."){ $images[$i]=

我有一个包含子目录的目录,在每个子目录中都有图像。我想随机显示图像。 下面是我用php编写的代码,它运行良好,但在Laravel中不起作用,问题在于
opendir()
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中,您需要使用它来处理文件系统

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

您可以使用Laravel的
allFiles
方法获取所有文件,并使用随机逻辑获取其中一幅图像

File::allFiles($directory)

实际上,您可以使用
Laravel文件系统
,但要使其完全工作,您必须设置配置。例如,以下代码将不起作用:

$dir = 'images'; // public/images
$files = \Storage::allFiles($dir);
因为,
文件系统
使用
config/filesystems.php
中的配置,其中您可能有如下内容:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
    // ...
]
$dir = 'images'; // public/images
if($files = \Storage::disk('web')->allFiles('images')) {
    $path = $files[array_rand($files)];
}
默认情况下,
Laravel
使用
local
磁盘,它指向不同的路径。因此,要使其工作,您必须在配置中设置磁盘,例如,在阵列中添加以下条目:

'web' => [
    'driver' => 'local',
    'root' => base_path('public'),
],
然后,您可以使用如下内容:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
    // ...
]
$dir = 'images'; // public/images
if($files = \Storage::disk('web')->allFiles('images')) {
    $path = $files[array_rand($files)];
}

要在
视图中使用此
$path
,请使用

下面是实现这一点的方法。我现在使用的是Laravel5.7,但应该也适用于较旧的版本

$files = Storage::files('path/to/directory');
$randomFile = array_random($files);

可以使用Laravel方法返回1个或多个随机元素

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

$rand = Arr::random($all, 1);
别忘了正面:

use Illuminate\Support\Arr;

检查,你可能会得到目录和子目录中的文件列表,然后可能会把随机顺序。回答这里我有错误:语法错误,意外',',期待']'现在我没有一个错误消息,但我没有收到任何在页面中,页面没有完全充电!!!如果使用计数($files),则会出现越界错误。需要将其更改为计数($files)-1。@robertmylne,在这种情况下,人们通常只是提出建议。没有必要对答案投反对票,这显然在某种程度上对你有所帮助。我已将其编辑为正确答案,没有潜在的错误,现在已投赞成票。