這篇文章主要介紹了php隨機顯示指定文件夾下圖片的方法,涉及array_rand隨機數組操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php隨機顯示指定文件夾下圖片的方法。分享給大家供大家參考。具體如下:
此代碼會從指定的服務器文件夾隨機選擇一個圖片進行顯示,非常有用,圖片格式為.gif,.jpg,.png
- <?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].'" />';
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答