0

Ok, no, this is not a dupe or at least it is not answered. I want to know the url to the currently executing script's directory.

Any answer involving $_SERVER['REQUEST_URI'], et al is (likely) off topic.

I have a script at /blah/blah/public_html/foo/bar/thescript.php. This script is included by other scripts. My browser is not at http://domain.com/foo/bar/thescript.php.

What I want is /foo/bar or http://domain.com/foo/bar

I came up with:

$dir = str_replace($_SERVER['DOCUMENT_ROOT'],'', __DIR__);

It works, ok. Just checking for other solutions.

Ok, yep, its a dupe. here is another way. (very much the same as what i came up with

$dir = substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT']));
Doug Cassidy
  • 1,796
  • 3
  • 17
  • 28
  • __DIR__ ? also worth calling phpinfo() into the script once, run it & poke around to see what choices you've got – Rob G Oct 08 '15 at 21:18
  • That's not meant to be bold. It's meant to be __ DIR __ without spaces – Rob G Oct 08 '15 at 21:19
  • dupe: http://stackoverflow.com/questions/2090723/how-to-get-the-relative-directory-no-matter-from-where-its-included-in-php –  Oct 08 '15 at 21:19
  • The method described above is indeed essentially a duplicate of the question identified by Dagon, however it is quite possible to map multiple different and separate directories into the same URL tree using Apache and other web servers even without the complications of mod_userdir and mod_rewrite. In short, there is no generic solution which works for all cases. – symcbean Oct 08 '15 at 23:07
  • Yes, it's possible, but is it at all likely, in the context of an average website, on an average server? I understand that there are many and varied ways of using LAMP, but I assume the 'normal' 'average' 'likely' way is serving a website. The specific context that I am working with is I am creating a module, which will be transferred from dev server to production, when it's all done. Also, this module will be deployed to other sites. (its a plugin). I want to know that when it gets put on prod server, the $dir is going to be the same. – Doug Cassidy Oct 09 '15 at 15:26

0 Answers0