I am writing installation script in PHP and part of my application may be installed in the folder that is one level above the public folder. Usually, public folder is called "public_html" but that may not always be the case. Is there some easy way to locate the name or path of the root public folder using PHP?
Asked
Active
Viewed 7,987 times
1
-
2How do you know your script will always have access to that folder, if it even exists? In any case, just use `../`. – Brad Feb 10 '12 at 03:35
-
True about permissions, but I figure I will deal with that separately. ../ may not work when my installation script runs on a sub-domain, which often has site root like public_html/subdomain/. Using ../ will take me up to public_html, but I need to go one level up. – igorilla Feb 10 '12 at 05:15
2 Answers
5
Try $_SERVER['DOCUMENT_ROOT']
. (documentation)
If you want to get the directory above it, you can use dirname
.
Brad does have a point, though; your script may not have access to that directory.
-
-
To make sure you can write to the folder above the web root ... `$levelUp = dirname($_SERVER['DOCUMENT_ROOT']); if (is_writable($levelUp)) { ... }` – Feb 10 '12 at 03:51
-
When a site is on subdomain, $_SERVER['DOCUMENT_ROOT'] gives me something like /home1/user/public_html/subdomain. And public_html can be called something else on different servers. – igorilla Feb 10 '12 at 05:19
0
$_SERVER['DOCUMENT_ROOT']
will provide the full path up topublic_html
and you can explode with slash "/" and get last index of the exploded array.- You can also use realpath with argument "../", if you don't know the actual file name. This will return you the full parent path of
public_html
.
Hope one of this two will work for you :)
-
1Thanks, but I don't know where my installation script is being ran from. It may be public_html/install.php, but it may also be from public_html/subfolder/install.php. For those two cases $_SERVER['DOCUMENT_ROOT'] will give me different results. And I can't look for "public_html" in either path because this directory may be called something else, like "www". – igorilla Feb 10 '12 at 05:27