【发布时间】:2012-09-21 21:10:29
【问题描述】:
我的网站有一个页面,用于存储参考图像..
目前我只是将所有图像放到我服务器上的一个目录中,然后 php 会以我喜欢的方式显示它们。
我想问的是如何让它们在每次刷新页面时以不同的随机顺序显示?
代码如下:
$dir = 'images';
$file_display = array ('jpg', 'jpeg', 'png', 'gif');
if (file_exists($dir) ==false) {
echo 'Directory \'', $dir, '\' not found';
} else {
$dir_contents = scandir($dir);
foreach ($dir_contents as $file) {
$file_type = strtolower(end(explode('.', $file)));
if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
echo '<img class="photo" src="', $dir, '/', $file, '" alt="', $file, '" />';
}
}
}
【问题讨论】:
标签: php image random directory