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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀