简体   繁体   English

php获取文件的根目录

[英]php get the root of the file

how can i get the root to the main folder? 我如何将根目录获取到主文件夹? for example i run file in this location : 例如我在此位置运行文件:

public_html/admin/user/etc/index.php

and i want to include file from 我想包含来自的文件

public_html/includes/user/etc/function.php

so i want to do something like this: 所以我想做这样的事情:

include(get_first_default_folder().'includes/user/etc/function.php');

instead of: 代替:

include('../../../includes/user/etc/function.php');

You can use Document root from $_SERVER['DOCUMENT_ROOT']. 您可以从$ _SERVER ['DOCUMENT_ROOT']使用文档根目录。

You can include this line in your index.php file. 您可以将此行包含在index.php文件中。

include("{$_SERVER['DOCUMENT_ROOT']}includes/user/etc/function.php");

I normally do this thusly. 我通常是这样做的。 $myDir = dirname(__FILE__); (or you can use __DIR__ on PHP v5.3+) is the directory name that the particular file is in. You know where in the hierarchy this is, so you can easily get the base with $myBaseDir = realpath('../../../'.$mydir) '. (或者您可以在PHP v5.3 +上使用__DIR__ )是特定文件所在的目录名称。您知道它在层次结构中的位置,因此您可以使用$myBaseDir = realpath('../../../'.$mydir) '。 Realpath expands the path to it's canonical version, not including any symbolic links. Realpath将路径扩展到其规范版本,不包括任何符号链接。 Having the full path can also allows for a small speedup when it comes to including or otherwise finding files. 拥有完整路径还可以在包括或以其他方式查找文件时加快速度。

The reason I do that, rather than using $_SERVER['DOCUMENT_ROOT'] , is that that environment variable probably won't exist if I'm running a script from the command line - it's set by the webserver. 我这样做而不是使用$_SERVER['DOCUMENT_ROOT']是,如果我从命令行运行脚本,则该环境变量可能不存在-由网络服务器设置。

This a bit clumsy approach I guess but it works for me. 我猜这有点笨拙,但对我有用。 On the beginning of every file I define a variable $root . 在每个文件的开头,我定义一个变量$root First level variable is $root = './' (this is not required thought), first subfolder is $root = '../' , second subfolder is $root = '../../' and so forth. 第一级变量$root = './' (这不是必需的思想),第一个子文件夹是$root = '../' ,第二子文件夹是$root = '../../'等等。 $root always takes me to the root folder of my site. $root总是将我带到站点的根文件夹。

When I need to provide a path I just do something like include("{$root}includes/user/etc/function.php"); 当我需要提供路径时,只需执行include("{$root}includes/user/etc/function.php");

This helped me to get rid of "no such index or file" warnings so I'll stick with it until I find a better way of doing the same thing. 这帮助我摆脱了“没有这样的索引或文件”警告,因此我会坚持下去,直到找到更好的方法来做同样的事情。

Alternatively you could define a constant for each folder level so you don't have to add $root at the top of the every file. 另外,您可以为每个文件夹级别定义一个常量,这样就不必在每个文件的顶部添加$root (Haven't tested it so I don't know if the following syntax is correct.) Instead you'd do something like: (没有测试过,所以我不知道以下语法是否正确。)相反,您可以执行以下操作:

  • {ROOT}includes/user/etc/function.php");` // root level files {ROOT} includes / user / etc / function.php“);`//根目录文件
  • {SUBFOLDER_LEVEL_ONE}includes/user/etc/function.php");` // first subfolder level files {SUBFOLDER_LEVEL_ONE} includes / user / etc / function.php“);`//第一个子文件夹级别的文件

After defining the constants somewhere: 在某处定义常量之后:

  • define("ROOT", "./"); define(“ ROOT”,“ ./”);
  • define("SUBFOLDER_LEVEL_ONE", "../"); define(“ SUBFOLDER_LEVEL_ONE”,“ ../”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM