php 如何获取public_html文件夹的绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6079479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the absolute path to the public_html folder?
提问by Can't Tell
$_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
问题是我必须编写一个脚本,该脚本可以位于 public_html 文件夹或 public_html 文件夹的子文件夹中。该脚本应将上传的文件保存到 public_html 目录中的文件夹中(比如图像)。换句话说,让我们说我的文件是 test.php 并且它的路径是
/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
必须保存通过 test.php 上传的文件。我不知道 test.php 文件将在哪里运行。例如它可以在路径上
/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?
如何在不知道 test.php 文件的位置的情况下获取图像文件夹的路径?
回答by totallyNotLizards
something I found today, after reading this question and continuing on my googlesurf:
在阅读了这个问题并继续在我的 googlesurf 上之后,我今天发现了一些东西:
https://docs.joomla.org/How_to_find_your_absolute_path
https://docs.joomla.org/How_to_find_your_absolute_path
<?php
$path = getcwd();
echo "This Is Your Absolute Path: ";
echo $path;
?>
works for me
为我工作
回答by Jim Rubenstein
Where is the file that you're running? If it is in your public html folder, you can do echo dirname(__FILE__);
你运行的文件在哪里?如果它在您的公共 html 文件夹中,您可以执行echo dirname(__FILE__);
回答by Jasom Dotnet
Let's asume that show_images.php
is in folder images
and the site root is public_html
, so:
让我们假设它show_images.php
在文件夹中images
,站点根目录是public_html
,所以:
echo dirname(__DIR__); // prints '/home/public_html/'
echo dirname(__FILE__); // prints '/home/public_html/images'
回答by Irman Ahmad
put anyfile on the directories you wanted to find, in this case, place 'root' at public_html
将 anyfile 放在您要查找的目录上,在这种情况下,将“root”放在 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
它会返回,例如php脚本的目录是
/home/user/public_html/test/another-dir/test.php
so the $pathToRoot will be
所以 $pathToRoot 将是
$pathToRoot => "../../../"
Is this the one you want??
这是你想要的吗??
回答by daalbert
回答by Sylverdrag
Out of curiosity, why don't you just use the url for the said folder?
出于好奇,你为什么不直接使用上述文件夹的 url?
Assuming that the folder never changes location, that would be the easiest way to do it.
假设文件夹永远不会改变位置,这将是最简单的方法。
回答by vinylDeveloper
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/
?>
回答by antelove
<?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/
回答by Daan van den Bergh
You can also use dirname in dirname to get to where you want to be.
您还可以在 dirname 中使用 dirname 来到达您想要的位置。
Example of usage:
用法示例:
For Joomla, modules will always be installed in /public_html/modules/mod_modulename/
对于 Joomla,模块将始终安装在 /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__)));
因此,从模块文件夹内的文件中,要访问任何服务器上的 Joomla install-root,我可以使用:
$path = dirname(dirname(dirname(__FILE__)));
The same goes for Wordpress, where plugins are always in wp-content/plugins/
Wordpress 也是如此,插件总是在 wp-content/plugins/
Hope this helps someone.
希望这可以帮助某人。
回答by NSD
with preg_replace function to find absolute path from __FILE__
you can easily find with anything home user. Here short my code :
使用 preg_replace 函数查找绝对路径,__FILE__
您可以轻松找到任何家庭用户。这里简短我的代码:
$path_image_you_want = preg_replace('#/public_html/([^/]+?)/.*#', '/public_html/$1/images",
__FILE__
);
$path_image_you_want = preg_replace('#/public_html/([^/]+?)/.*#', '/public_html/$1/images",
__FILE__
);