How to get random image from directory using PHP

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

    $folder = "images";
    $results_img_arr = array();
    if (is_dir($folder))
    {
            if ($handle = opendir($folder))
            {
                    while(($file = readdir($handle)) !== FALSE)
                    {
                        if(!in_array($file,array(".","..")))
                            $results_img_arr[] = $folder."/".$file;
                   }
             closedir($handle);
            }
    }
    $ran_img_key  = array_rand($results_img_arr);
    
    $img_path = $results_img_arr[$ran_img_key];
    

提交回复
热议问题