include - Store last folder name in path to a variable in PHP -


i have php script call "functions.php" executed both , other scripts placed in other folders. therefore encounter problem code tries include database file. @ first came quick lazy work around. amde script ask twice same file this:

include('../database_file.php'); include('../../database_file.php');  

however, not solution. makes warning every time, since can't load 1 of files. have therefore decided make switch statement reads if functions.php file called or somewhere else , include file correct path. (if there better workaround, please tell.)

i use following code read current path, can make if statement decides wherefrom database file should loaded.

realpath(dirname(__file__)); 

this return path similar this:

/var/www/website/public

how can store last folder in path variable / remove before last /?

what using absolute path? in way not care actual folder, file loaded anyway. example

include('/var/www/website/public/path_to_file.php'); 

edit

since stated cannot use absolute path can use explode function on path's variables become array, it's easy last entry of array wich last folder, , store in variable

$path = realpath(dirname(__file__)); $folder_array = explode('/', $path); $i = count($folder_array) - 1; $folder = $folder_array[$i]; echo 'folder is: ' . $folder; 

this output

folder is: public 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -