PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function getRandomTxtFile($directory) { // 确保目录路径以斜杠结尾 $directory = rtrim($directory, '/') . '/'; // 打开目录 $dir = opendir($directory); if (!$dir) { return "无法打开目录。"; } $txtFiles = array(); // 读取目录内容 while (($file = readdir($dir)) !== false) { if (is_file($directory . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'txt') { $txtFiles[] = $directory . $file; } } closedir($dir); // 检查是否有 txt 文件 if (empty($txtFiles)) { return "没有找到 txt 文件。"; } // 随机选择一个文件 $randomFile = $txtFiles[array_rand($txtFiles)]; // 返回文件的完整路径 return $randomFile; } // 使用示例 $directory = 'sitemapall/kaiyuncc'; $randomTxtFile = getRandomTxtFile($directory); echo $randomTxtFile;
Show:  
Copy Clear