dongmozhui3805 2015-11-04 22:56
浏览 50
已采纳

包含网站目录的文件 - 制作导航始终包含connect.php,无论子目录是什么[重复]

This question already has an answer here:

I've been working on a small site, to work on my PHP Skills. I've got the navigation inside of a sub-folder called Templates. Now basically, the navigation is going to be included on every page, and along with it connect.php. Now the problem is, if it goes into another sub-folder then it isn't going to be able to find the connect.php file. I've got the APP Directory setup, and that is working on the Stylesheet and all.

However I'm not sure how I would do the include, I got the idea/code for the link href from StackOverflow on another question and on that it said that include (APP_DIR .'connect.php') however that doesn't seem to work for me, it brings up an error "Warning: include(APP_ROOT/connect.php): failed to open stream: No such file or directory in C:\xampp\htdocs\ytsite\templates avigation.php on line 2 "

Any help on how to correctly include a file so no matter where it is, it includes the connect would be much appreciated.

Thanks, Ben

</div>
  • 写回答

1条回答 默认 最新

  • douzi9744 2015-11-04 23:22
    关注

    Your constant should be a directory path, not a url. Try this with your constant definition..

    define('APP_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/ytsite' );
    
    echo APP_ROOT; //to test
    

    Not tested. Beware you don't get double slashes and make sure the path goes to the correct directory. Also make sure you add a slash when you include 'connect.php'. Include it in the constant definition, or add it to your include line.

    include (APP_ROOT .'/connect.php');
    

    edit

    So include your connect file like this:

    Make a master file to include into your pages and add these lines.

    <?php
        define('APP_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/ytsite' );
        include (APP_ROOT .'/connect.php');
    ?>
    

    Every time you launch a new page, the constant needs to be recreated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c