包含PHP的PHP问题

时间:2011-07-24 13:35:01

标签: php

<?
$dir=scandir('/home/crusty/www/crusty.bshellz.pl/htdocs/404/');

foreach($dir as $file){
        if($file!='.' && $file!='..' && $file!='index.php'){
                $choice=$dir[rand(0, count($dir) - 1)];
                include($choice);
        }
}
?>

我的代码有点问题。当然它正在处理一些文件,但它仍然试图包括index.php,..和。 可以帮我解决一下吗?

4 个答案:

答案 0 :(得分:0)

您必须提供完整路径,尝试从脚本位置包含文件。

改变这个:

include($choice);

为:

include('/home/crusty/www/crusty.bshellz.pl/htdocs/404/'.$choice);

我不会这样做,但它应该有用。

答案 1 :(得分:0)

将您的代码分成两部分:第一部分准备一系列好文件;第二个包含随机文件:

$allfiles = scandir('/home/crusty/www/crusty.bshellz.pl/htdocs/404/');

$goodfiles = array();
foreach ($allfiles as $file) {
  if($file!='.' && $file!='..' && $file!='index.php'){
    $goodfiles[] = $file;
  }
}

$choicenfile = $goodfiles[rand(0, count($goodfiles) - 1)];
// As I understant You want to include only one file, not all;
include($choicenfile);

现在您甚至可以将此代码提取到方法或函数

答案 2 :(得分:0)

我不确定您是要以随机顺序包含所有文件还是只包含给定文件夹的一个随机文件,因此我已将两者都包含在解决方案中 - 只需删除您不需要的文件:

function filter_includes($incfile) {
    return !in_array($incfile, array(".", "..", "index.php"));
}

$dirPath = '/home/crusty/www/crusty.bshellz.pl/htdocs/404/';
$dir = array_filter(scandir($dirPath), "filter_includes");

// include all files in randomized order
shuffle($dir);
foreach($dir as $file) {
    include($dirPath . $file);
}

// include one random file
include($dirPath . $dir[rand(0, count($dir) - 1)]);

答案 3 :(得分:0)

$choice=$dir[rand(0, count($dir) - 1)];中的兰特有什么意义?

因为现在它只是在你的数组中包含一个随机文件。

您应该将代码更改为:

$dir=scandir('/home/crusty/www/crusty.bshellz.pl/htdocs/404/');

foreach($dir as $file){
    if($file!='.' && $file!='..' && $file!='index.php'){
        include($file);
    }
}