How to get random image from directory using PHP

前端 未结 10 1752
我寻月下人不归
我寻月下人不归 2020-12-02 19:16

I have one directory called images/tips.

Now in that directory I have many images which can change.

I want the PHP script to read the directory, to find the

10条回答
  •  伪装坚强ぢ
    2020-12-02 19:41

    Agreed with alexa. Use simple function.

    function RandImg($dir)
    {
    $images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    $randomImage = $images[array_rand($images)];
    return $randomImage;
    }
    
    $the_image = RandImg('images/tips/');
    echo $the_image;
    

提交回复
热议问题