duanshaiduhao2471 2016-03-18 12:45
浏览 24
已采纳

PHP:包含来自未知文件夹的已知文件名

I am making a website the tech-tree of a game a friend is making. The tech-three itself is just a list of links going from one page to the other. The variables for each page are stored in a php file e.g.: mining.php. I don't want to have a massive list of files all in one folder. So far all I've managed to do is sort them into just the first branches of the tree with this:

$path = glob('tree/*/'.$page.'.php');
if ($path[0]) {
    include $path[0];

Ideally I want to be able to access the file from anywhere in the tree.
e.g.: The file mining.php would be at tree/strength/mining.php. And the file pickaxe.php would be at tree/strength/mining/pickaxe.php. But I would be able to include either one with the same include. What could i use to get my known filename from any folder or subfolder in the tree folder.

  • 写回答

3条回答 默认 最新

  • dongzhen6554 2016-03-18 23:48
    关注

    I worked out an answer for myself (it took me a few hours but this will make it easier to sort through the hundred of files that i will eventually have):

    $count = 0;
    $path = 'tree/*/';
    $file = glob($path.$page.'.php');
    while ((strpos($file[0], $page) == false) and ($count != 10)) {
        $file = glob($path.$page.'.php');
        $path .= '*/';
        $count++;
    }
    if (strpos($file[0], $page) == true) {
        include $file[0];
    

    Just make sure you know how many folders you have and make (count == #) higher.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序