dsdvr06648 2016-08-22 19:11
浏览 37

php包含使用子目录时的路径

I'm working with an existing site, where the files are set up like this:

(index.php)

include_once("functions/Functions.php");
...

(Functions.php)

function GetAValue()
{
   include_once("settings/settings.php");
}

So the index file calls functions in a function folder, and the sql variables and whatnot are stored in a settings folder like so:

(folder) functions 
(folder) settings
(file) index.php

My question is this: I want to add a new working directory, mobile. If I use include_once("../functions/functions.php");, it will error when the function tries to include the settings file, as it is looking for "mobile/settings/settings.php" which doesn't exist.

What are my options here ?

  • 写回答

1条回答 默认 最新

  • dourong8495 2016-08-22 19:58
    关注

    I always use a config file that's stored in the public root. In the config I define a Constant for the base root and one for public root.

    <?php
    // myconfig.php -- stored in the public root aka document root
    $direx = explode("/", getcwd());
    DEFINE ('BASEROOT', '/'.$direx[1].'/'.$direx[2].'/'); // host/mysite/
    DEFINE ('PUBLICROOT', '/'.$direx[1].'/'.$direx[2].'/'.$direx[3].'/'); // host/mysite/public_html/
    ?>
    

    require the config at the top of all pages.

    <?php
    require_once('myconfig.php'); // called on every page
    
    //example uses
    IF (File_Exists(PUBLICROOT.'some_folder/file.txt')) { /* do something */ }
    
    include_once(PUBLICROOT.'some_folder/file.txt');
    ?>
    

    To include the config in a document root / subdir scenario you can use the following.

    Require(str_replace("subdir", "", getcwd()).'myconfig.php');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致