PHP: Pop-Back File Traversing (For Special Cases)
I'm working on an app (WIODE of course...) and trying to build a method for linking back to the config file. Problem is that it's not a definite. Plus, now I'm trying to set it when symlink'ing to the system folder. Here's a little function to handle this...
Instead of:
require_once('../../../config.php');
...which works well UNLESS I'm using symlinked assets, I use:
function changeDir($up_n){
$split = explode("/",$_SERVER['SCRIPT_FILENAME']);
array_pop($split); // Remove current file name
for ($i=1; $i<=$up_n; $i++){ array_pop($split); }
return implode("/",$split);
}
require_once(changeDir(3)."/config.php");
Pretty simple, works no matter where the DOCUMENT_ROOT is or if I'm using a symlink.
Nothing fancy, but it works well. No matter where I install the application or how I'm linking to the system files it works (dig into the WIODE code if you want an example).
Responses to this Article:
Loading Comments...
I am a web developer, designer, and consultant located in the La Crosse / Onalaska Wisconsin region with
over twelve years experience developing and managing projects ranging from large applications and cloud-based
business solutions to social/new media campaigns, to complete system and infrastructure implementation.