2011-07-24 143 views
0
<? 
$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,..和。 同一个人可以帮我解决吗?包括PHP的问题

回答

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); 

现在,你甚至可以提取该代码的方法或功能

0

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

更改此:

include($choice); 

到:

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

我不会做这种方式,但它应该工作。

+0

警告:包括(/home/crusty/www/crusty.bshel​​lz.pl/htdocs)[]:未能打开流:不适当ioctl for device in /home/crusty/www/crusty.bshel​​lz.pl/htdocs/404/index.php on line 8 Warning:include()[function.include]:失败打开'/ home/crusty/www/'包含在/home/crusty/www/crusty.bshel​​lz.pl/中的'crusty.bshel​​lz.pl/htdocs/404/ ..'(include_path ='。:/ usr/share/php5:/ usr/share/php') htdocs/404/index.php上线8 所以它仍然不工作。 – crusty

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)]); 
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); 
    } 
}