【发布时间】:2015-02-10 16:06:02
【问题描述】:
我必须从 PHP 的目录中随机选择一个文件,假设有三个文件,比如说 index.php 、 a.php 和 b.php。如何确保我不选择文件 index.php 而是随机选择其他文件。 到目前为止,我有以下代码
$dir = 'uploads';
$files = glob($dir . '/*.php');
$file = array_rand($files);
echo $files[$file];
【问题讨论】:
-
...通过在您的目录中没有 index.php 包含不应返回 index.php 的随机文件...确定吗?
-
添加一个if来检查
$files[$file]是否不等于index.php -
我无法移动 index.php
-
检查应该是
$dir.index.php== $files[$file]`还是index.php== $files[$file]` -
忘记了一个 not :D,我编辑了我之前的评论