dqxm14187 2015-04-03 16:27
浏览 75
已采纳

在循环中包含变量文件名

with $result ok and $inclusion being an actual file name

I would like to do this :

while ($row = mysqli_fetch_array($result)){

 $inclusion = $row['filename'] ;

 // check if file exists here

 include $inclusion ;

}

But it does not loop ... and instead stops after the first inclusion ... why ? Thank you !

  • 写回答

1条回答 默认 最新

  • duanchuan6350 2015-04-03 17:19
    关注

    This should work for you:

    Here I first add all files to the array $files. After this I array_filter() all files out which doesn't exists and take only unique files with array_unique().

    At the end I just loop through all files with array_map() then since you don't have the path form the server root, but from the server disk, I just take the basename() (means the file name) and concatenate it with __DIR__ to include it.

    <?php
    
        while ($row = mysqli_fetch_array($result)){
            $files[] = $row['filename'];
        }
    
        $files = array_unique(array_filter($files, function($v){
            return file_exists($v);
        }));
    
        array_map(function($v){
            require_once(__DIR__ . "/" . basename($v));
        }, $files);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?