[英]PHP include random file from folder
I would like to use a <?php include...
command to choose one random php file from one folder.我想使用
<?php include...
命令从一个文件夹中选择一个随机 php 文件。
So this is the basic idea:所以这是基本思想:
<?php include 'random file: 1.php, 2.php, 3.php or 4.php';?>
I have already read articles like this or this , but they don't answer my question clearly.我已经阅读过类似this或this的文章,但它们并没有清楚地回答我的问题。 What is the easiest way to do that?
最简单的方法是什么?
// Directory to use
$directory = '.';
// Filter out directories, we only want files.
$files = array_filter(scandir($directory), fn($f) => is_file($f));
// Pick a random file
$randFile = $directory . '/' . $files[array_rand($files)];
// Include it
include $randFile;
to include random files from another directory包含来自另一个目录的随机文件
$path = __DIR__."/folder";
foreach(new DirectoryIterator($path) as $file){
if($file->isFile()){
$arr[] = $file->getFilename();
}
}
$randFile = $path."/".$arr[array_rand($arr)];
include $randFile;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.