33
$_SERVER['DOCUMENT_ROOT']

returns

/usr/local/apache/htdocs/

is there a way to get

/home/user/public_html/

The problem is that I have to write a script which can be in the public_html folder or a sub-folder of the public_html folder. The script should save uploaded files into a folder inside public_html directory(say images). In other words let us say that my file is test.php and the path to it is

/home/user/public_html/test/test.php. 

And there is a folder

/home/user/public_html/images 

where files uploaded via test.php have to be saved. I don't know where the test.php file will be run. For example it can be at the path

/home/user/public_html/test/another-dir/test.php

How do I get the path to the images folder without knowing where the test.php file will be?

Can't Tell
  • 12,714
  • 9
  • 63
  • 91

11 Answers11

45

something I found today, after reading this question and continuing on my googlesurf:

https://docs.joomla.org/How_to_find_your_absolute_path

<?php
$path = getcwd();
echo "This Is Your Absolute Path: ";
echo $path;
?>

works for me

hatef
  • 5,491
  • 30
  • 43
  • 46
totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
21

Where is the file that you're running? If it is in your public html folder, you can do echo dirname(__FILE__);

Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54
  • I don't know where it will be run. That is the problem. I want to make it portable. It should be able to run it anywhere, but the folder that I need to access will be inside the public_html folder. – Can't Tell May 21 '11 at 04:14
  • 1
    Will it always be named public HTML? because you could do: `$folder = dirname(__FILE__); while (basename($folder) != 'public_html') $folder = dirname($folder);` and that'd get you there, but if you're running that every time the page loads? that sucks - plus, it needs some checking to make sure you don't loop infinitely (if public_html is never found). ps sorry for the delayed response..i only just saw this comment ): – Jim Rubenstein Jun 11 '12 at 20:51
13

Let's asume that show_images.php is in folder images and the site root is public_html, so:

echo dirname(__DIR__); // prints '/home/public_html/'
echo dirname(__FILE__); // prints '/home/public_html/images'
Jasom Dotnet
  • 1,225
  • 12
  • 17
3

put anyfile on the directories you wanted to find, in this case, place 'root' at public_html

/home/user/public_html/root <- note that 'root' is not a folder (you can use root.txt if u want)

And use this function

function findThis($get){
    $d = '';
    for($i = 0; $i < 20; $i++){//this will try 20 times recursively on upper folder
        if(file_exists($d.$get)){
            return $d;
        }else{
            $d.="../";
        }
    }
}

and get the value by calling it

$pathToRoot = findThis('root');

And it will return, for example the the dir of php script is

/home/user/public_html/test/another-dir/test.php

so the $pathToRoot will be

$pathToRoot => "../../../"

Is this the one you want??

Irman Ahmad
  • 182
  • 3
  • 12
  • I don't have access to the server. So I can't place a "root" file.What I'm writing is a plugin. Thanks anyway. What I want to know is whether it is possible to do this using php code only. It's quite surprising that there is no easy way of doing this. – Can't Tell May 21 '11 at 06:46
  • then maybe you can take a wild guess by append '../' at the file you want to find in the images.. like "../images/this.jpg".. if it fails, try "../../images/this.jpg" and so on.. once you got it.. it can be use.. – Irman Ahmad May 21 '11 at 06:59
2

This is super old, but I came across it and this worked for me.

<?php
//Get absolute path
$path = getcwd();
//strip the path at your root dir name and everything that follows it
$path = substr($path, 0, strpos($path, "root"));
echo "This Is Your Absolute Path: ";
echo $path; //This will output /home/public_html/
?>
vinylDeveloper
  • 687
  • 2
  • 7
  • 25
2
<?php

    // Get absolute path
    $path = getcwd(); // /home/user/public_html/test/test.php.   

    $path = substr($path, 0, strpos($path, "public_html"));

    $root = $path . "public_html/";

    echo $root; // This will output /home/user/public_html/
antelove
  • 3,216
  • 26
  • 20
2

Out of curiosity, why don't you just use the url for the said folder?

http://www.mysite.com/images

Assuming that the folder never changes location, that would be the easiest way to do it.

Sylverdrag
  • 8,898
  • 5
  • 37
  • 54
  • 2
    Because you can't write files via the URL. – Ignacio Vazquez-Abrams May 21 '11 at 04:49
  • I want to get the path "/home/user/public_html/images". The problem is that the "user" can change. So I can't hard code it.(The name of the public_html directory can also be different.) – Can't Tell May 21 '11 at 04:50
  • @Ignacio: Outch! I didn't realize that. – Sylverdrag May 21 '11 at 04:55
  • @Can't Tell: Do you have access to the user name? Without it, what you are asking looks impossible, and with it, can't you just write "/home/$user/public_html/images"? – Sylverdrag May 21 '11 at 05:16
  • The path might not contain the user name at all. What I need is the path to the directory from which the files are served. – Can't Tell May 21 '11 at 05:33
  • @Can't Tell: So, what information do you have? How do you decide where something is supposed to go? The problem with this question is that you are asking for a magic way to get a path to various folders which are not defined in any way. From what I understand, your scripts are called by logged in users, right? Well, on login, save the user's paths in $_SESSION and use that each time you need to make an operation (not forgetting of course to check that the user is authorized to perform whatever operation he is performing). – Sylverdrag May 21 '11 at 05:46
  • The information that I have is the folder name "images". What my question is about is, "how to get the absolute path to the directory from which the files are served". If that path is say $x, then my path is $x."/images" – Can't Tell May 21 '11 at 05:56
  • 1
    @Can't tell: In that case you have no way of doing that. What is there are several "images" folders on the server? How are you going to decide which one is the right one? I STRONGLY suspect that you are trying to solve the wrong problem. Is your software something that will be installed on various servers beyond your control? Create an installation script and use it to configure your folder paths, either by creating the folder or by asking the user to provide the path to an existing folder. If it is on a server you can control, simply create the folders you need and use those. Don't guess. – Sylverdrag May 21 '11 at 09:49
  • Thanks for your suggestions. I will look more into the problem to figure out an alternative. – Can't Tell May 21 '11 at 10:40
2

Whenever you want any sort of configuration information you can use phpinfo().

daalbert
  • 1,465
  • 9
  • 7
2

You just need to create an offset bypass to how far you want the backwards reading to go. So, we use getcwd() to get the path and explode (split into array) to fetch the data between $root and the ending of the path.

function getRoot($root = "public_html") {
    return explode($root, getcwd())[0].$root."/";
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

with preg_replace function to find absolute path from __FILE__ you can easily find with anything home user. Here short my code :

$path_image_you_want = preg_replace('#/public_html/([^/]+?)/.*#', '/public_html/$1/images", __FILE__);

NSD
  • 1
  • 1
0

You can also use dirname in dirname to get to where you want to be.

Example of usage:

For Joomla, modules will always be installed in /public_html/modules/mod_modulename/

So, from within a file within the module's folder, to get to the Joomla install-root on any server , I could use: $path = dirname(dirname(dirname(__FILE__)));

The same goes for Wordpress, where plugins are always in wp-content/plugins/

Hope this helps someone.