2

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.

I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.

Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.

The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.

Like this: /home/user/file.php

needs to detect

/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.

Winston
  • 76
  • 1
  • 4

2 Answers2

5

The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.

One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.

<?php
    if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
            echo $_SERVER["DOCUMENT_ROOT"]; 
    }

Then within your file.php your would do something like

$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));

This makes the assumption that the server can resolve to itself via the local interface by name.

Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • The problem is the file is in the root directory, but needs to detect the public_html folder. If I used `$_SERVER["DOCUMENT_ROOT"]` from the root directory, wouldn't that just give me the root folder? – Winston Jun 21 '13 at 05:41
  • $_SERVER["DOCUMENT_ROOT"] == the file system path when you to to `http://www.mydomain.com/` – Orangepill Jun 21 '13 at 05:46
  • I think I get what you are after based on the comment above, is it true that this is not being executed by the webserver itself but rather as a command line script or via cron? – Orangepill Jun 21 '13 at 05:48
  • I think he might be executing the script with the terminal, thus, the document root is not the same as the one found within apache. – Dave Chen Jun 21 '13 at 05:48
  • @Winston you may be able to use a `~` file reference if the directory is located in the user's home directory. try `~/public_html/` – Orangepill Jun 21 '13 at 05:51
  • So what's happening is that I'm using Server #1 to connect to Server #2 via FTP within PHP, uploading a file to server #2 root directory, then run the file in the root directory. But, the file uploaded to the server #2 needs the public_html file path to do what I need it to do. – Winston Jun 21 '13 at 05:52
  • @Orangepill That would work if it's called public_html, but for some hosts it is different like `/var/www/` or `/website.com/html/` etc. – Winston Jun 21 '13 at 05:53
  • Thank you for $_SERVER["REMOTE_ADDR"] you saved my time :) :x – Mahdi Jazini Jan 19 '17 at 16:07
  • the problem with this almost perfect solution is that it only works for either HTTP or HTTPS, but not both. so the same code won't work on my production server and my actual server if they use different protocols... – Abraham Murciano Benzadon Jul 06 '17 at 14:58
0

I found this website which provided me with the only good solution I have found after scouring the web...

$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);

The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.