How to get random image from directory using PHP

前端 未结 10 1774
我寻月下人不归
我寻月下人不归 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:51

    Load folder with images:

    $folder = opendir(images/tips/);
    

    Build table out of files/images from directory:

    $i = 0;
    while(false !=($file = readdir($folder))){
    if($file != "." && $file != ".."){
        $images[$i]= $file;
        $i++;
        }
    }
    

    Pick random:

    $random_img=rand(0,count($images)-1);
    

    Show on page:

    echo '';
    

    Hope it helps. Of course enclose it in .

提交回复
热议问题