PHP paths from within included files -
scenario
i have 2 files: file.php
, admin/file.php
file.php
has require_once 'admin/functions.php';
admin/file.php
has require_once 'functions.php';
so they're both including same file. trouble within functions.php
, things file_get_contents
, include
, require
see defined paths relative file.php
, admin/file.php
, not functions.php
.
because of this, can't use same functions.php
different directories without creating duplicate files don't think best thing doing incase edit one, i'll have remember edit other well.
what's best practice type of situation?
edit: see comment on accepted answer below.
what's best practice type of situation?
easy. set base path manually in config file & don’t worry it—relative paths—ever again.
that said, if trying simplify way files loaded, should explicitly set $base_path
this:
$base_path = '/full/path/to/your/codebase/here/';
if don’t know file system base path is, place line of code in php code; index.php
:
echo "your path is: " . realpath(dirname(__file__)) . "<br />";
then load page. somewhere near top text:
your path is: /full/path/to/your/codebase/here/
then set can change code this:
and set include_once
this:
include_once $base_path . 'includes/myfile.php';
some might should use $_server['document_root']
or dirname(__file__)
directly implication being can simplify code portability way. way file paths set installs can vary never works & chances of getting snagged on odd server quirk high.
it’s best manually set $base_path
in config file when move code deal headaches caused php constants $_server
not being consistent between installs, setups & configurations.
Comments
Post a Comment