How to get the absolute path to the public_html folder?
Asked Answered
C

11

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?

Crenation answered 21/5, 2011 at 3:49 Comment(4)
Ask the question you really want answered, not the question you think will answer it.Kushner
Try to do var_dump($_SERVER); and see what's defined.Sheeb
@Ignacio I made some changes. Is the question more clear now?Neoptolemus
No. All you've done is buried yourself deeper.Kushner
P
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

Pentstemon answered 2/5, 2012 at 15:51 Comment(0)
M
21

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

Mechelle answered 21/5, 2011 at 3:59 Comment(2)
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.Neoptolemus
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 ):Mechelle
H
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'
Hephzipa answered 3/5, 2016 at 13:47 Comment(1)
this is the correct answer. path to php must be specified correctly.Shroudlaid
M
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??

Marconigraph answered 21/5, 2011 at 6:39 Comment(2)
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.Neoptolemus
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..Marconigraph
F
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.

Foucault answered 21/5, 2011 at 4:48 Comment(9)
Because you can't write files via the URL.Kushner
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.)Neoptolemus
@Ignacio: Outch! I didn't realize that.Foucault
@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"?Foucault
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.Neoptolemus
@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).Foucault
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"Neoptolemus
@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.Foucault
Thanks for your suggestions. I will look more into the problem to figure out an alternative.Neoptolemus
L
2

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

Landsman answered 22/5, 2011 at 0:35 Comment(0)
T
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/
?>
Talavera answered 4/11, 2015 at 23:48 Comment(0)
C
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/
Canvasback answered 29/8, 2019 at 13:57 Comment(0)
S
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."/";
}
Sikora answered 26/9, 2020 at 7:56 Comment(0)
G
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__);

Gaming answered 10/2, 2013 at 2:46 Comment(0)
U
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.

Unstained answered 6/7, 2016 at 14:17 Comment(2)
Not that in wordpress plugins CAN be in a different folder.Frosted
That's true. However, the above solution still works, as long as the plugins directory is one directory deep from root.Unstained

© 2022 - 2024 — McMap. All rights reserved.