开发者

Define absolute path for files

开发者 https://www.devze.com 2023-02-25 01:24 出处:网络
I have the following include files.. require_once (\"config.php\"); require_once (\"functions.php\"); require_once (\"session.php\");

I have the following include files..

require_once ("config.php");
require_once ("functions.php");
require_once ("session.php");

I want to define absolute paths for my include files. I have tried with the following code and no luck..

can you please help to define an appropriate absolute path, so that require_once as expected.

    defined('DS') ? null : define('DS',DIRECTORY_SEPARAT开发者_Python百科OR);
    defined('SITE_ROOT') ? null: define('SITE_ROOT',DS.'PHP_Files'. DS . 'phpsandbox'. DS.'my_mat'. DS.'my_test');
    defined('LIB_PATH')?null:define('LIB_PATH',SITE_ROOT.DS.'includes');

    require_once(LIB_PATH.DS."config.php");
    require_once(LIB_PATH.DS."functions.php");
    require_once(LIB_PATH.DS."session.php");

These 3 include files in my system are stored in J:\PHP_Files\phpsandbox\my_mat\my_test\includes.

Thanks in advance!


Ok, so I think what you are looking for is the actual system file path. To get that you can echo

dirname( __FILE__ ); 

You can do this in any file that you want and it will display the system file path relative to your file. For me it's something like this:

/home2/myusername/public_html/project_name/includes/config.php

so if you are interested in the "project_name" folder you should have something like this:

defined("SITE_ROOT") ? null : define("SITE_ROOT", DS . "home2" . DS . "myusername" . DS . "public_html" . DS . "project_name" );

Then if you are looking for the "includes" folder which will be your library you should have something like this:

defined("LIB_PATH")  ? null : define("LIB_PATH", SITE_ROOT . DS . "includes" );

Hope this helps. I had the exact same problem and this worked for me.

Cheers, Mihai Popa


Try including your LIB_PATH in your include path.

set_include_path(LIB_PATH . DS . PATH_SEPARATOR . get_include_path());


require_once(dirname(__FILE__).DS."config.php");
require_once(dirname(__FILE__).DS."functions.php");
require_once(dirname(__FILE__).DS."session.php");

check it out , i think it's good


You need realpath function. Also, you can get all files included by get_included_files that returns array of absolute paths of files you've included at the moment function's got called.

defined('DS') or define('DS',DIRECTORY_SEPARATOR);
$disk_label = '';
if (PHP_OS == 'WINNT') {
    if (FALSE !== ($pos = strpos(__FILE__, ':'))) {
        $disk_label = substr(__FILE__, 0,$pos+1);
    }
}
defined('SITE_ROOT') or define('SITE_ROOT', $disk_label.DS.'PHP_Files'. DS . 'phpsandbox'. DS.'my_mat'. DS.'my_test');
defined('LIB_PATH') or define('LIB_PATH',SITE_ROOT.DS.'includes');

require_once(LIB_PATH.DS."config.php");
require_once(LIB_PATH.DS."functions.php");
require_once(LIB_PATH.DS."session.php");
0

精彩评论

暂无评论...
验证码 换一张
取 消