In HTML, I can find a file starting from the web server's root 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 page is saying that I can set include_path
once 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't want.
相对包含正是我不想要的。
How do I make sure that all my includes point to the root/includes
folder? (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_path
thing 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! :)
无论如何,问题解决了! :)
10 个解决方案
#1
44
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,但它使你的其他包括更容易。
#2
24
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");
#3
8
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.
因此,为了确保您的应用程序始终包含您的路径(如果该文件存在),只需将include目录放在目录列表中。
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.
这样,首先搜索include目录,然后搜索原始搜索路径(默认为当前目录,然后是PEAR)。如果您在修改include_path时没有问题,那么这就是您的解决方案。
#4
2
There is nothing in include/require that 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)
应该工作得很好。假设路径和文件是corect并且设置了正确的权限。 (从而允许您在文档根目录内外包含您喜欢的任何文件)
This behaviour is however considered to be a possible security risk. Therefore, the system administrator can set the open_basedir directive.
但是,这种行为被认为是一种可能的安全风险。因此,系统管理员可以设置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
#5
2
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_PATH
as an absolute path for MyProject.
现在,您只需要在MyProyect文件夹中的每个文件中包含此文件(my_paths.php),这样您就可以将MY_ABS_PATH用作MyProject的绝对路径。
#6
0
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中使用include with allow_url_include时要注意,当从包含文件访问会话时,如果从脚本中使用绝对文件引用包含一个文件,然后使用url文件引用包含来自本地服务器的第二个文件具有不同的变量范围,并且不会从两个包含的文件中看到相同的会话。从包含url的文件中将看不到原始会话。
from: http://us2.php.net/manual/en/function.include.php#84052
来自:http://us2.php.net/manual/en/function.include.php#84052
#7
0
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系统遇到了类似的问题。我需要一些安全方面的艰难道路。认为最好的方式就像抢写道。为了快速编写脏代码认为这也有用.. :-)
<?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
美好的一天strtok
#8
0
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");
#9
0
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 document root - XAMPP default)而不是/
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(“../../ includedme.php”)
AND NOT include("/myapp/includeme.php")
并且不包括(“/ myapp / includeme.php”)
#10
0
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“../”从文件所在的任何位置升级到层次结构。每当我想要包含absolut路径时,该行将是:
<?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运行和文件放置的位置,使其独立于系统。要访问网络目录之外的文件,请在$ root之后添加更多../,例如$根。 “../外部/ file.txt的”。
#1
44
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,但它使你的其他包括更容易。
#2
24
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");
#3
8
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.
因此,为了确保您的应用程序始终包含您的路径(如果该文件存在),只需将include目录放在目录列表中。
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.
这样,首先搜索include目录,然后搜索原始搜索路径(默认为当前目录,然后是PEAR)。如果您在修改include_path时没有问题,那么这就是您的解决方案。
#4
2
There is nothing in include/require that 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)
应该工作得很好。假设路径和文件是corect并且设置了正确的权限。 (从而允许您在文档根目录内外包含您喜欢的任何文件)
This behaviour is however considered to be a possible security risk. Therefore, the system administrator can set the open_basedir directive.
但是,这种行为被认为是一种可能的安全风险。因此,系统管理员可以设置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
#5
2
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_PATH
as an absolute path for MyProject.
现在,您只需要在MyProyect文件夹中的每个文件中包含此文件(my_paths.php),这样您就可以将MY_ABS_PATH用作MyProject的绝对路径。
#6
0
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中使用include with allow_url_include时要注意,当从包含文件访问会话时,如果从脚本中使用绝对文件引用包含一个文件,然后使用url文件引用包含来自本地服务器的第二个文件具有不同的变量范围,并且不会从两个包含的文件中看到相同的会话。从包含url的文件中将看不到原始会话。
from: http://us2.php.net/manual/en/function.include.php#84052
来自:http://us2.php.net/manual/en/function.include.php#84052
#7
0
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系统遇到了类似的问题。我需要一些安全方面的艰难道路。认为最好的方式就像抢写道。为了快速编写脏代码认为这也有用.. :-)
<?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
美好的一天strtok
#8
0
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");
#9
0
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 document root - XAMPP default)而不是/
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(“../../ includedme.php”)
AND NOT include("/myapp/includeme.php")
并且不包括(“/ myapp / includeme.php”)
#10
0
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“../”从文件所在的任何位置升级到层次结构。每当我想要包含absolut路径时,该行将是:
<?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运行和文件放置的位置,使其独立于系统。要访问网络目录之外的文件,请在$ root之后添加更多../,例如$根。 “../外部/ file.txt的”。