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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 如何推断此服务器配置
  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败