PHP Scandir returns extra periods

前端 未结 4 468
深忆病人
深忆病人 2021-01-15 07:53

So I am trying to build a script that scans a directory and returns random images to be used as backgrounds.

The php looks like this:

$dir = \"views         


        
4条回答
  •  渐次进展
    2021-01-15 08:31

    Use glob() so you can filter the files.

    $files = glob('views/img/bg/*.jpg');
    $random = $files[array_rand($files)];
    

    Since you're specifying *.jpg, $files contains only JPG files and you don't need to remove the . and .. items.

提交回复
热议问题