Jump to content

__FILE__ to


P3t3r

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/82979-__file__-to/
Share on other sites

  • 3 weeks later...

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?

Link to comment
https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433516
Share on other sites

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?
Link to comment
https://forums.phpfreaks.com/topic/82979-__file__-to/#findComment-433658
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.