問題
../health/というファイルからランダムなページを表示しようとしています。このファイルには、index.phpファイルとphpファイルという名前の118個の他のファイルがあります。健康フォルダからランダムにファイルを表示したいのですが、index.phpファイルを除外したいと思います。
この次のコードには、index.phpファイルが含まれている場合があります。$exclude行を変更して../health/index.phpを表示しようとしましたが、それでもうまくいきません。
<?php
$exclude = array("index.php"); // can add more here later
$answer = array_diff(glob("../health/*.php"),$exclude);
$whatanswer = $answer[mt_rand(0, count($answer) -1)];
include ($whatanswer);
?
私が試した別のコードは次のとおりです
<?php
$exclude = array("../health/index.php"); // can add more here later
$health = glob("../health/*.php");
foreach ($health as $key => $filename) {
foreach ($exclude as $x) {
if (strstr($filename, $x)) {
unset($whathealth[$key]);
}
}
}
$whathealth = $health[mt_rand(0, count($health) -1)];
include ($whathealth);
?>
このコードにはindex.phpファイルも含まれていますが、ページを表示するのではなく、ページをエラーとして表示します。