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 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路