2011-07-24 198 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); 
    } 
}