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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.