如何在 PHP 中设置绝对包含路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/344419/
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 do I set an absolute include path in PHP?
提问by Nathan Long
In HTML, I can find a file starting from the web server'sroot folder by beginning the filepath with "/". Like:
在 HTML 中,我可以通过以“/”开头的文件路径找到从Web 服务器的根文件夹开始的文件。喜欢:
/images/some_image.jpg
I can put that path in any file in any subdirectory, and it will point to the right image.
我可以将该路径放在任何子目录中的任何文件中,它会指向正确的图像。
With PHP, I tried something similar:
使用 PHP,我尝试了类似的东西:
include("/includes/header.php");
...but that doesn't work.
......但这不起作用。
I think that that this pageis saying that I can set include_pathonce and after that, it will be assumed. But I don't quite get the syntax. Both examples start with a period, and it says:
我认为这个页面是说我可以设置include_path一次,然后,它会被假设。但我不太明白语法。这两个例子都以句点开头,它说:
Using a . in the include path allows for relative includes as it means the current directory.
用一个 。在包含路径中允许相对包含,因为它意味着当前目录。
Relative includes are exactly what I don'twant.
相对包含正是我不想要的。
How do I make sure that all my includes point to the root/includesfolder?(Bonus: what if I want to place that folder outside the public directory?)
如何确保我的所有包含都指向该root/includes文件夹?(奖励:如果我想将该文件夹放在公共目录之外怎么办?)
Clarification
澄清
My development files are currently being served by XAMPP/Apache. Does that affect the absolute path? (I'm not sure yet what the production server will be.)
我的开发文件目前由 XAMPP/Apache 提供服务。这会影响绝对路径吗?(我还不确定生产服务器是什么。)
Update
更新
I don't know what my problem was here. The include_paththing I referenced above was exactly what I was looking for, and the syntax isn't really confusing. I just tried it and it works great.
我不知道我这里的问题是什么。include_path我上面引用的东西正是我正在寻找的东西,而且语法并不令人困惑。我刚试过,效果很好。
One thing that occurs to me is that some people may have thought that "/some/path" was an "absolute path" because they assumed the OS was Linux. This server is Windows, so an absolute path would have to start with the drive name.
我想到的一件事是,有些人可能认为“/some/path”是“绝对路径”,因为他们认为操作系统是 Linux。此服务器是 Windows,因此绝对路径必须以驱动器名称开头。
Anyway, problem solved! :)
总之,问题解决了!:)
回答by Rob Prouse
What I do is put a config.php file in my root directory. This file is included by all PHP files in my project. In that config.php file, I then do the following;
我所做的是在我的根目录中放置一个 config.php 文件。该文件包含在我项目中的所有 PHP 文件中。在那个 config.php 文件中,我执行以下操作;
define( 'ROOT_DIR', dirname(__FILE__) );
Then in all files, I know what the root of my project is and can do stuff like this
然后在所有文件中,我知道我的项目的根是什么并且可以做这样的事情
require_once( ROOT_DIR.'/include/functions.php' );
Sorry, no bonus points for getting outside of the public directory ;) This also has the unfortunate side affect that you still need a relative path for finding config.php, but it makes the rest of your includes much easier.
抱歉,离开公共目录没有奖励积分;) 这也有一个不幸的副作用,您仍然需要一个相对路径来查找 config.php,但它使您的其余包含更容易。
回答by Nathan Long
One strategy
一种策略
I don't know if this is the best way, but it has worked for me.
我不知道这是否是最好的方法,但它对我有用。
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/path/to/file.php");
回答by gnud
The include_path setting works like $PATH in unix (there is a similar setting in Windows too).It contains multiple directory names, seperated by colons (:). When you include or require a file, these directories are searched in order, until a match is found or all directories are searched.
include_path 设置的工作方式类似于 unix 中的 $PATH(在 Windows 中也有类似的设置)。它包含多个目录名称,以冒号 (:) 分隔。当您包含或需要一个文件时,将按顺序搜索这些目录,直到找到匹配项或搜索所有目录。
So, to make sure that your application always includes from your path if the file exists there, simply put your include dir first in the list of directories.
因此,要确保您的应用程序始终从您的路径中包含文件(如果文件存在),只需将您的包含目录放在目录列表中。
ini_set("include_path", "/your_include_path:".ini_get("include_path"));
This way, your include directory is searched first, and then the original search path (by default the current directory, and then PEAR). If you have no problem modifying include_path, then this is the solution for you.
这样,您的包含目录将被首先搜索,然后是原始搜索路径(默认为当前目录,然后是 PEAR)。如果修改 include_path 没有问题,那么这就是适合您的解决方案。
回答by Jacco
There is nothing in include/requirethat prohibits you from using absolute an path. so your example
include/ require中没有任何内容禁止您使用绝对路径。所以你的例子
include('/includes/header.php');
should work just fine. Assuming the path and file are corect and have the correct permissions set.
(and thereby allow you to include whatever file you like, in- or outside your document root)
应该工作得很好。假设路径和文件是正确的并且设置了正确的权限。
(从而允许您在文档根目录内外包含您喜欢的任何文件)
This behaviour is however considered to be a possible security risk. Therefore, the system administrator can set the open_basedirdirective.
然而,这种行为被认为是一种可能的安全风险。因此,系统管理员可以设置open_basedir指令。
This directive configures where you can include/require your files from and it might just be your problem.
Some control panels (plesk for example) set this directive to be the same as the document root by default.
该指令配置您可以从哪里包含/要求您的文件,这可能只是您的问题。
默认情况下,某些控制面板(例如 plesk)将此指令设置为与文档根目录相同。
as for the '.'syntax:
至于'.'语法:
/home/username/public_html <- absolute path public_html <- relative path ./public_html <- same as the path above ../username/public_html <- another relative path
However, I usually use a slightly different option:
但是,我通常使用稍微不同的选项:
require_once(__DIR__ . '/Factories/ViewFactory.php');
With this edition, you specify an absolute path, relative to the file that contains the require_once()statement.
使用此版本,您可以指定相对于包含require_once()语句的文件的绝对路径。
回答by kunde
Another option is to create a file in the $_SERVER['DOCUMENT_ROOT']directory with the definition of your absolute path.
另一种选择是在$_SERVER['DOCUMENT_ROOT']具有绝对路径定义的目录中创建一个文件。
For example, if your $_SERVER['DOCUMENT_ROOT']directory is
例如,如果您的$_SERVER['DOCUMENT_ROOT']目录是
C:\wamp\www\
create a file (i.e. my_paths.php) containing this
创建一个my_paths.php包含这个的文件(即)
<?php if(!defined('MY_ABS_PATH')) define('MY_ABS_PATH',$_SERVER['DOCUMENT_ROOT'].'MyProyect/')
Now you only need to include in every file inside your MyProyect folder this file (my_paths.php), so you can user MY_ABS_PATHas an absolute path for MyProject.
现在您只需要在 MyProyect 文件夹中的每个文件中包含此文件 ( my_paths.php),这样您就可以将用户MY_ABS_PATH用作 MyProject 的绝对路径。
回答by kunde
hey all...i had a similar problem with my cms system. i needed a hard path for some security aspects. think the best way is like rob wrote. for quick an dirty coding think this works also..:-)
大家好...我的cms系统也有类似的问题。对于某些安全方面,我需要一条艰难的道路。认为最好的方法是像 rob 写的。为了快速进行肮脏的编码,认为这也有效..:-)
<?php
$path = getcwd();
$myfile = "/test.inc.php";
/*
getcwd () points to: /usr/srv/apache/htdocs/myworkingdir (as example)
getcwd() 指向:/usr/srv/apache/htdocs/myworkingdir(例如)
echo ($path.$myfile);
would return...
/usr/srv/apache/htdocs/myworkingdir/test.inc.php
access outside your working directory is not allowed.
*/
includ_once ($path.$myfile);
//some code
?>
nice day strtok
美好的一天
回答by Dinah
Not directly answering your question but something to remember:
不是直接回答你的问题,而是要记住一些事情:
When using includes with allow_url_include on in your ini beware that, when accessing sessions from included files, if from a script you include one file using an absolute file reference and then include a second file from on your local server using a url file reference that they have different variable scope and the same session will not be seen from both included files. The original session won't be seen from the url included file.
在您的 ini 中使用带 allow_url_include 的包含时请注意,从包含的文件访问会话时,如果从脚本中使用绝对文件引用包含一个文件,然后使用 url 文件引用包含本地服务器上的第二个文件具有不同的变量范围,并且从两个包含的文件中都不会看到相同的会话。从包含的 url 文件中看不到原始会话。
from: http://us2.php.net/manual/en/function.include.php#84052
回答by Tyler Carter
I follow Wordpress's example on this one. I go and define a root path, normally the document root, and then go define a bunch of other path's along with that (one for each of my class dirs. IE: database, users, html, etc). Often I will define the root path manually instead of relying on a server variable.
我在这个例子中遵循 Wordpress 的例子。我去定义一个根路径,通常是文档根,然后去定义一堆其他路径(一个用于我的每个类目录。IE:数据库、用户、html 等)。通常我会手动定义根路径而不是依赖服务器变量。
Example
例子
if($_SERVER['SERVERNAME'] == "localhost")
{
define("ABS_PATH", "/path/to/upper/most/directory"); // Manual
}
else
{
define("ABS_PATH, dirname(__FILE__));
// This defines the path as the directory of the containing file, normally a config.php
}
// define other paths...
include(ABS_PATH."/mystuff.php");
回答by Aveesh
Thanks - this is one of 2 links that com up if you google for php apache windows absolute path.
谢谢 - 这是 2 个链接之一,如果您在 google 上搜索 php apache windows 绝对路径。
As a newbie to intermed PHP developer I didnt understand why absolute paths on apache windopws systems would be c:\xampp\htdocs (apache document root - XAMPP default) instead of /
作为中间 PHP 开发人员的新手,我不明白为什么 apache windopws 系统上的绝对路径会是 c:\xampp\htdocs(apache 文档根 - XAMPP 默认)而不是 /
thus if in http//localhost/myapp/subfolder1/subfolder2/myfile.php I wanted to include a file from http//localhost/myapp
因此,如果在 http//localhost/myapp/subfolder1/subfolder2/myfile.php 我想包含来自 http//localhost/myapp 的文件
I would need to specify it as: include("c:\xampp\htdocs\myapp\includeme.php") or include("../../includeme.php")
我需要将其指定为:include("c:\xampp\htdocs\myapp\includeme.php") 或 include("../../includeme.php")
AND NOT include("/myapp/includeme.php")
并且不包括(“/myapp/includeme.php”)
回答by nishluck
I've come up with a single line of code to set at top of my every php script as to compensate:
我想出了一行代码来设置在我的每个 php 脚本的顶部以进行补偿:
<?php if(!$root) for($i=count(explode("/",$_SERVER["PHP_SELF"]));$i>2;$i--) $root .= "../"; ?>
By this building $root to bee "../" steps up in hierarchy from wherever the file is placed. Whenever I want to include with an absolut path the line will be:
通过构建 $root to bee "../",从文件所在的位置开始逐步提升层次结构。每当我想包含绝对路径时,该行将是:
<?php include($root."some/include/directory/file.php"); ?>
I don't really like it, seems as an awkward way to solve it, but it seem to work whatever system php runs on and wherever the file is placed, making it system independent.
To reach files outside the web directory add some more ../after $root, e.g. $root."../external/file.txt".
我不太喜欢它,这似乎是一种笨拙的解决方法,但它似乎可以在任何系统 php 上运行以及放置文件的任何位置工作,使其独立于系统。到达Web目录以外的文件添加多一些../后$root,如$root."../external/file.txt"。

