0

Getting the root to the site's folder isn't difficult but I am trying to get the folder in which all sites are located rather than the root folder of a specific site. I have been using the code below but I came across an instance affecting $CommonPath where it isn't working due to one site's specific structure.

In other words, one of my Windows development systems has something like:

C:\Server\Sites\site1.loc
C:\Server\Sites\site2.loc
C:\Server\Sites\site3.loc

and another Linux-based development system has:

/var/www/html/site1.loc
/var/www/html/site2.loc
/var/www/html/site3.loc

and I am trying to fetch C:\Server\Sites\ or /var/www/html/ from code that is inside a subfolder or two from these roots. How is it done without having to specify which subfolder the variable is being created in?

$SiteFolder = @end(explode(DIRECTORY_SEPARATOR, dirname(__DIR__)));
$CommonPath = str_replace($SiteFolder,"",dirname(__DIR__)) . "common" . DIRECTORY_SEPARATOR;
$ConfigPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR;
$FunctionsPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . "functions" . DIRECTORY_SEPARATOR;
DonP
  • 725
  • 1
  • 8
  • 27
  • `$_SERVER['DOCUMENT_ROOT']`? – Professor Abronsius Jan 20 '18 at 07:58
  • All domains having a common base path is only valid for 08/15 vhost setups. You would have to traverse Apache config and includes to uncover the main pool. (Whether it's old-school `/var/www/*` or `/home/user*/htdocs` or `/srv/example.com*` or even just `/git.biz*` and alike, or any combination thereof.) – mario Jan 20 '18 at 08:01
  • is `C:\Server\Sites\site1.dev` the document root for the site `site1.dev`? and likewise for the others – Professor Abronsius Jan 20 '18 at 08:32
  • Yes, in this example that is correct. $_SERVER['DOCUMENT_ROOT'] gives C:/Server/Sites/site1 but I want C:/Server/Sites/ – DonP Jan 20 '18 at 09:13

3 Answers3

0

You can look at this:

realpath — Returns canonicalized absolute pathname

http://php.net/manual/en/function.realpath.php

This will return false if the path/file does not exists.

<?php
   echo realpath('/windows/system32');

   echo realpath('C:\Program Files\\');
?>

Output:

C:\WINDOWS\System32
C:\Program Files


Added Reference:
This may also help you: Get 'My Documents' path in windows using php

Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
  • Yes but I need to do it both in Windows and in Linux without specifying any path. Obviously I could type in the path manually but I need the code to be more portable where it will work across these various development systems and, of course, the live Linux server which is different yet from these examples. – DonP Jan 20 '18 at 08:12
0

I finally got it working by first creating a variable for the site's root, then exploding on the directory separator and getting only the last bit, then filtering it out of the path. Perhaps it's not the most elegant code but it seems to work.

$dirSeparator = DIRECTORY_SEPARATOR;
$serverRoot = str_replace("/",$dirSeparator,$_SERVER['DOCUMENT_ROOT']);
$SiteFolder = end(explode($dirSeparator,$serverRoot));
$CommonPath = str_replace($SiteFolder,"",$serverRoot);

I'm not sure why I was unable to use DIRECTORY_SEPARATOR directly but creating a variable for it did work. Also, and possibly a PHP bug, using $_SERVER['DOCUMENT_ROOT'] on a Windows server still gives Linux directory separators so I had to do a str_replace() to work that out.

The reason I needed to do all this was to be able to include files with functions common to several sites that are in an aliased folder so my actual code is like this:

$CommonPath = str_replace($SiteFolder,"",$serverRoot) . "common" . $dirSeparator;
DonP
  • 725
  • 1
  • 8
  • 27
0

Using the "stfu operator" (@) with end() is not recommended when avoidable.

I might recommend string functions rather than temporary array manipulation:

Code: (Demo)

$document_roots = [
    "C:\Server\Sites\site1.dev",
    "C:/Server/Sites/site1",
    "/var/www/html/site1.dev"
];

foreach ($document_roots as $document_root) {
    $prepared = str_replace("\\","/",$document_root);
    echo substr($prepared,0,strrpos($prepared,"/"));
    echo " or ";
    echo preg_replace("~/[^/]*$~","",$prepared);
    echo "\n";
}

Output:

C:/Server/Sites or C:/Server/Sites
C:/Server/Sites or C:/Server/Sites
/var/www/html or /var/www/html
mickmackusa
  • 43,625
  • 12
  • 83
  • 136