P3t3r Posted December 23, 2007 Share Posted December 23, 2007 I'd like to make a file that recognizes its own relative path. So if the file is named /var/www/vhosts/mysite.com/httpdocs/dirname/folder1/folder2/test.php, then it should create a variable $relpath="folder1/folder2/test.php". Of course substr(__FILE__,36) and then removing the dirname works, but that's not quite dynamic. What is the 'correct' way to do this? Shortest code (in bytes) preferred. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/ Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 define("_ROOT_", "/var/www/vhosts/mysite.com/httpdocs/dirname/"); $relpath= _ROOT_ . "folder1/folder2/test.php" Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-422032 Share on other sites More sharing options...
trq Posted December 23, 2007 Share Posted December 23, 2007 Your much better off using absolute paths anyways. Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-422038 Share on other sites More sharing options...
P3t3r Posted January 8, 2008 Author Share Posted January 8, 2008 Ok, I'll be more specific. I have folders /var/www/vhosts/mysite.com/httpdocs/dirname/folder1/folder2/test.php, /var/www/vhosts/mysite.com/httpdocs/dirname2/folder3/folder4/test.php, /var/www/vhosts/mysite.com/httpdocs/dirname3/folder5/folder6/test.php... and I'd like to extract the /dirname/ from it (more specificly replace them all with /folder0/). I could make a list of all possible names for httpdocs/httpsdocs/httpd/... and math the part after that, but I guess there must be a better way? Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433516 Share on other sites More sharing options...
rajivgonsalves Posted January 8, 2008 Share Posted January 8, 2008 you can use the following echo dirname(__FILE__); hope its what you wanted... Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433526 Share on other sites More sharing options...
duclet Posted January 8, 2008 Share Posted January 8, 2008 Set up a root the root path, then use str_replace to remove it from the __FILE__ and you will get the path relative to the root: <?php $root = '/your/root/folder'; $relative = str_replace($root, '', __FILE__); ?> Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433562 Share on other sites More sharing options...
P3t3r Posted January 8, 2008 Author Share Posted January 8, 2008 Set up a root the root path, then use str_replace to remove it from the __FILE__ and you will get the path relative to the root: <?php $root = '/your/root/folder'; $relative = str_replace($root, '', __FILE__); ?> Is there a way to find the root path dynamically? My host seems to swap servers regularly. Or would this consume too much execution time? Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433658 Share on other sites More sharing options...
duclet Posted January 8, 2008 Share Posted January 8, 2008 The only thing I can come up with is $_SERVER['DOCUMENT_ROOT'] but I am not sure how reliable that is. Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433858 Share on other sites More sharing options...
rajivgonsalves Posted January 9, 2008 Share Posted January 9, 2008 you should do $relative = str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__); Quote Link to comment https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-434295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.