文章摘要
这篇文章描述了一段PHP代码,用于从指定文件夹中获取所有.gif、.jpg和.png格式的图片,并随机选择一张图片进行展示。代码使用`glob`函数生成包含所有图片文件名的数组,然后使用`array_rand`随机选择一张图片,最后通过`echo`在网页上显示选定的图片。关键词包括“获取图片数组”、“指定格式”、“随机选择”和“显示图片”。
<?php //This will get an array of all the gif, jpg and png images in a folder $img_array = glob("/path/to/images/*.{gif,jpg,png}",GLOB_BRACE); //Pick a random image from the array $img = array_rand($img_array); //Display the image on the page echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />'; ?>