Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
function getRandomTxtFile($directory) {
// 确保目录路径以斜杠结尾
$directory = rtrim($directory, '/') . '/';
// 获取目录中所有的 txt 文件
$files = glob($directory . '*.txt');
// 检查是否有 txt 文件
if (empty($files)) {
return "没有找到 txt 文件。";
}
// 随机选择一个文件
$randomFile = $files[array_rand($files)];
// 返回文件的完整路径
return $randomFile;
}
// 使用示例
$directory = 'sitemapall/kaiyuncc';
$randomTxtFile = getRandomTxtFile($directory);
echo $randomTxtFile;