【发布时间】:2019-04-16 21:20:25
【问题描述】:
我正在尝试从随机目录中选择随机图像。我创建函数来获取随机目录和另一个函数从该目录中获取随机图像。好的,但它不起作用,它获取随机目录和来自另一个目录的随机图像
<?php
function showRandomDir()
{
$files = glob('images/portfolio/*/', GLOB_ONLYDIR);
shuffle($files);
$files = array_slice($files, 0, 1);
foreach($files as $file)
{
return $file;
}
}
function rotate2()
{
$list = scandir(showRandomDir());
$fileRotateList = array();
$img = '';
foreach($list as $fileRotate)
{
if (is_file(showRandomDir() . htmlspecialchars($fileRotate)))
{
$ext = strtolower(pathinfo($fileRotate, PATHINFO_EXTENSION));
if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png')
{
$fileRotateList[] = $fileRotate;
}
}
}
if (count($fileRotateList) > 0)
{
$imageNumber = time() % count($fileRotateList);
$img = showRandomDir() . urlencode($fileRotateList[$imageNumber]);
}
return $img;
}
【问题讨论】:
-
我认为随机是一个神话:imgs.xkcd.com/comics/random_number.png
标签: php image random directory