如何使用PHP从目录中获取随机图像 [英] How to get random image from directory using PHP
本文介绍了如何使用PHP从目录中获取随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为images/tips的目录.
I have one directory called images/tips.
现在在该目录中,我可以更改很多图像.
Now in that directory I have many images which can change.
我希望PHP脚本读取目录,查找图像,然后从那些图像中选择一个随机图像.
I want the PHP script to read the directory, to find the images, and out of those images found to pick a random image.
关于如何执行此操作的任何想法?
Any idea on how to do this?
推荐答案
$imagesDir = 'images/tips/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)]; // See comments
您可以向array_rand()
发送第二个参数以获取大于1的值.
You can send a 2nd argument to array_rand()
to get more than 1.
这篇关于如何使用PHP从目录中获取随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文