Randomly select file in PHP

女生的网名这么多〃 提交于 2020-01-11 10:33:31

问题


I'd like to use the 'include' keyword in php to randomly select a file from a folder and output the contents. How would I do this?

Thanks.


回答1:


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);
}



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/2844272/randomly-select-file-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!