【问题标题】:Why is require_once is nulling variable name on last iteration of readdir()?为什么 require_once 在 readdir() 的最后一次迭代中将变量名归零?
【发布时间】:2014-01-10 16:05:32
【问题描述】:

为什么 require_once 在 readdir() 的最后一次迭代中清除变量名?

$added = array();
$folder = 'database';
$contents = opendir($folder);

while($filename = readdir($contents)){
    if(! is_dir($filename) ) {//if not dir
        echo '<BR/><BR/>';
        var_dump($filename);
        echo '<BR/>';
        $added[] = $folder.'/'.$filename;
        require_once($folder.'/'.$filename);
        var_dump($filename);
    }
}

我得到以下输出:

string(8) "init.php" 
string(8) "init.php" 

string(14) "dbContract.php" 
string(14) "dbContract.php" 

string(12) "requires.php" 
NULL

代替:

string(8) "init.php" 
string(8) "init.php" 

string(14) "dbContract.php" 
string(14) "dbContract.php" 

string(12) "requires.php" 
string(12) "requires.php" 

但是,当我注释掉该行时,我确实得到了预期的输出:

require_once($folder.'/'.$filename);

【问题讨论】:

  • 您排除了 .和..?
  • 可能requires.php 中的某些内容覆盖了$filename 的值
  • 谢谢,就是这样@Mark

标签: php file directory


【解决方案1】:

requires.php 中的某些内容正在更改 $filename 谢谢@马克

编辑 我想我明白了:

function requireDir($folder, $subs, &$added){

    $contents = opendir($folder);

    while($filename = readdir($contents)){
        $wholename = $folder.'/'.$filename;

        if(is_dir($wholename)){
            if($filename != '.' && $filename != '..'){
                if($subs){
                    requireDir($wholename,$added);
                }
            }
        }else if(is_file($wholename)){
            $added[] = $wholename;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多