开发者

Randomly select file in PHP

开发者 https://www.devze.com 2022-12-30 17:17 出处:网络
I\'d like to use the \'include\' keyword i开发者_开发知识库n php to randomly select a file from a folder and output the contents. How would I do this?

I'd like to use the 'include' keyword i开发者_开发知识库n php to randomly select a file from a folder and output the contents. How would I do this?

Thanks.


Assuming you know the folder where the files are and that the files are PHP files:

$phpFiles = glob('/path/to/files/*.php');

if (empty($phpFiles) === false)
{
    $randomFile = $phpFiles[array_rand($phpFiles)];
    include($randomFile);
}


glob() would read filenames into array.
and then you can shuffle this array and pick a random item.


use glob to get an array of files. shuffle the array. array_shift the first file off of the array. Include it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号