How to get the absolute path to the public_html fo

2019-01-17 07:10发布

$_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?

9条回答
可以哭但决不认输i
2楼-- · 2019-01-17 07:31

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

查看更多
别忘想泡老子
3楼-- · 2019-01-17 07:36

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/
?>
查看更多
倾城 Initia
4楼-- · 2019-01-17 07:38

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

查看更多
登录 后发表回答