I’m looking for a way to detect only the path of a folder that a file resides in.
If you did echo FILE;
You might get something like: /var/www/html/wonshikee/index.php
Now lets say /var/www/html is the root (http://www.example.com would go to /var/www/html/index.php)
Then /wonshikee is the path
And index.php is obviously the file name. (http://www.example.com/wonshikee/index.php)
Generally, $_SERVER[‘DOCUMENT_ROOT’] works, but this does not work in every server configuration, so it’s not the correct solution.
So is there a “correct” way to parse out the path?
Q: Why would you ever need to know this?
If you know the absolute path to where you are, and where everything in your app is relative to the current code, you can construct any path to a file and any URL for a website.
What does it matter if it may be difficult to figure out what the document root of the domain is?
PHP function dirname() may solve your problem.
Well I use a custom framework, and having it automatically detect its correct location means, a site can be moved and all paths will work, both file and http.
$path = rtrim(DIRECTORY_SEPARATOR.ltrim(str_replace(array($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR.'core'.DIRECTORY_SEPARATOR.'inc'), '', dirname(__FILE__)), DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR);
That is what I was using before, but on godaddy accounts, it was invalid and had to be hard coded to get it to work. It works for most servers, so I’m just trying to find a solution that works for all.
DIR should work everywhere:
'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
'Sensio' => __DIR__.'/../vendor/bundles',
'JMS' => __DIR__.'/../vendor/bundles',