I have a directory structure like the following;
我有一个目录结构,如下所示;
script.php
script.php
inc/include1.php
inc/include2.php公司/ include1。php公司/ include2.php
objects/object1.php
objects/object2.php对象/中的object1。php对象/ object2.php
soap/soap.php
soap / soap.php
Now, I use those objects in both script.php
and /soap/soap.php
, I could move them, but I want the directory structure like that for a specific reason. When executing script.php
the include path is inc/include.php
and when executing /soap/soap.php
it's ../inc
, absolute paths work, /mnt/webdev/[project name]/inc/include1.php...
But it's an ugly solution if I ever want to move the directory to a different location.
现在,我在两个脚本中都使用了这些对象。php和/ soap /肥皂。php,我可以移动它们,但是我想要这样的目录结构。在执行脚本。包含路径是inc/include。执行/soap/soap时。php的. ./inc,绝对路径工作,/mnt/webdev/[项目名称]/inc/include1.php…但如果我想将目录移动到不同的位置,这是一个很糟糕的解决方案。
So is there a way to use relative paths, or a way to programically generate the "/mnt/webdev/[project name]/"
?
那么是否有一种方法可以使用相对路径,或者有一种方法可以编程地生成“/mnt/webdev/[项目名称]/”?
11 个解决方案
#1
140
This should work
这应该工作
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/inc/include1.php";
Edit: added imporvement by aussieviking
编辑:添加了奥斯西维克的进项
#2
41
You can use relative paths. Try __FILE__
. This is a PHP constant which always returns the path/filename of the script it is in. So, in soap.php
, you could do:
可以使用相对路径。__FILE__试试。这是一个PHP常量,它总是返回它所在脚本的路径/文件名。所以,在肥皂。php,你能做的:
include dirname(__FILE__).'/../inc/include.php';
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2,
__FILE__
always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances. (source)文件的完整路径和文件名。如果在包含中使用,则返回包含文件的名称。由于PHP 4.0.2, __FILE__总是包含一个解析符号链接的绝对路径,而在旧版本中,它在某些情况下包含相对路径。(源)
Another solution would be to set an include path in your httpd.conf or an .htaccess file.
另一种解决方案是在httpd中设置包含路径。conf或.htaccess文件。
#3
7
have a look at http://au.php.net/reserved.variables
查看http://au.php.net/reserved.variables
I think the variable you are looking for is: $_SERVER["DOCUMENT_ROOT"]
我认为您正在寻找的变量是:$_SERVER["DOCUMENT_ROOT"]
#4
6
You could define a constant with the path to the root directory of your project, and then put that at the beginning of the path.
您可以使用项目的根目录的路径定义一个常量,然后将其放在路径的开头。
#5
4
Another way to handle this that removes any need for includes at all is to use the autoload feature. Including everything your script needs "Just in Case" can impede performance. If your includes are all class or interface definitions, and you want to load them only when needed, you can overload the __autoload()
function with your own code to find the appropriate class file and load it only when it's called. Here is the example from the manual:
另一种处理此问题的方法是使用autoload特性,这样就完全不需要include了。包括脚本需要的一切“以防万一”可能会影响性能。如果您的include是所有类或接口定义,并且您只希望在需要时加载它们,那么您可以使用您自己的代码重载__autoload()函数,以找到适当的类文件,并只在调用它时加载它。下面是手册中的例子:
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
As long as you set your include_path variables accordingly, you never need to include a class file again.
只要您相应地设置include_path变量,就永远不需要再包含类文件。
#6
2
Another option, related to Kevin's, is use __FILE__
, but instead replace the php file name from within it:
另一个与Kevin相关的选项是使用__FILE__,但是要从其中替换php文件名:
<?php
$docRoot = str_replace($_SERVER['SCRIPT_NAME'], '', __FILE__);
require_once($docRoot . '/lib/include.php');
?>
I've been using this for a while. The only problem is sometimes you don't have $_SERVER['SCRIPT_NAME']
, but sometimes there is another variable similar.
我用这个已经有一段时间了。唯一的问题是,有时您没有$_SERVER['SCRIPT_NAME'],但有时还有另一个类似的变量。
#7
2
I found this to work very well!
我发现这个很有效!
function findRoot() {
return(substr($_SERVER["SCRIPT_FILENAME"], 0, (stripos($_SERVER["SCRIPT_FILENAME"], $_SERVER["SCRIPT_NAME"])+1)));
}
Use:
使用:
<?php
function findRoot() {
return(substr($_SERVER["SCRIPT_FILENAME"], 0, (stripos($_SERVER["SCRIPT_FILENAME"], $_SERVER["SCRIPT_NAME"])+1)));
}
include(findRoot() . 'Post.php');
$posts = getPosts(findRoot() . 'posts_content');
include(findRoot() . 'includes/head.php');
for ($i=(sizeof($posts)-1); 0 <= $i; $i--) {
$posts[$i]->displayArticle();
}
include(findRoot() . 'includes/footer.php');
?>
#8
1
I think the best way is to put your includes in your PHP include path. There are various ways to do this depending on your setup.
我认为最好的方法是在PHP include路径中加入include。根据您的设置,有多种方法可以实现这一点。
Then you can simply refer to
然后你可以参考
require_once 'inc1.php';
from inside any file regardless of where it is whether in your includes or in your web accessible files, or any level of nested subdirectories.
从任何文件的内部,无论它在您的包含中还是在您的web可访问文件中,或任何级别的嵌套子目录中。
This allows you to have your include files outside the web server root, which is a best practice.
这允许您在web服务器根之外拥有您的包含文件,这是一种最佳实践。
e.g.
如。
site directory
html (web root)
your web-accessible files
includes
your include files
Also, check out __autoload for lazy loading of class files
另外,请查看__autoload,以延迟加载类文件。
http://www.google.com/search?q=setting+php+include+path
http://www.google.com/search?q=setting + php +有+路径
http://www.google.com/search?q=__autoload
http://www.google.com/search?q=__autoload
#9
1
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");
I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.
我使用这一行代码。它返回站点树的“顶部”,然后返回所需的文件。
For example, let's say i have this file tree:
例如,假设我有这个文件树:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.phpdomain.com/aaa/index.php domain.com/bbb/ccc/ddd/index.php domain.com/_resources/functions.php
I can include the functions.php file from wherever i am, just by copy pasting
我可以包含函数。php文件,无论我在哪里,只是复制粘贴。
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
If you need to use this code many times, you may create a function that returns the str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))
part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named path_back
):
如果需要多次使用此代码,可以创建一个函数,返回str_repeat('../'、(substr_count(getenv('SCRIPT_URL')、'/')-1)部分。然后在包含的第一个文件中插入这个函数。我有一个“初始化。php“文件,我在每个php页面的最顶端包含这个函数。下次我需要包含文件时,我实际上只是使用了函数(名为path_back):
require(path_back()."/_resources/another_php_file.php");
#10
0
@Flubba, does this allow me to have folders inside my include directory? flat include directories give me nightmares. as the whole objects directory should be in the inc directory.
@Flubba,这是否允许我的include目录中包含文件夹?扁平目录给我带来噩梦。因为整个对象目录应该在inc目录中。
Oh yes, absolutely. So for example, we use a single layer of subfolders, generally:
哦,是的,当然。例如,我们使用一层子文件夹,通常是:
require_once('library/string.class.php')
You need to be careful with relying on the include path too much in really high traffic sites, because php has to hunt through the current directory and then all the directories on the include path in order to see if your file is there and this can slow things up if you're getting hammered.
你需要小心依靠包括路径太多真的高流量的网站,因为php亨特通过当前目录,然后所有的目录包括路径为了看你的文件,这可以缓慢的东西如果你得到了。
So for example if you're doing MVC, you'd put the path to your application directoy in the include path and then specify refer to things in the form
举个例子,如果你在做MVC,你会把你的应用程序directoy的路径放在include路径中然后指定引用表单中的东西
'model/user.class'
'controllers/front.php'
or whatever.
之类的。
But generally speaking, it just lets you work with really short paths in your PHP that will work from anywhere and it's a lot easier to read than all that realpath document root malarkey.
但是一般来说,它只是让你使用短路径在PHP将从任何地方访问,而且它比这一切更容易读realpath文档根胡说。
The benefit of those script-based alternatives others have suggested is they work anywhere, even on shared boxes; setting the include path requires a little more thought and effort but as I mentioned lets you start using __autoload which just the coolest.
其他人提出的基于脚本的替代方案的好处是,它们可以在任何地方工作,甚至可以在共享框上工作;设置包含路径需要更多的思考和努力,但是正如我提到的,允许您开始使用__autoload,这是最酷的。
#11
0
If you are going to include specific path in most of the files in your application, create a Global variable to your root folder.
如果要在应用程序中的大多数文件中包含特定路径,请为根文件夹创建一个全局变量。
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../app'));
or
define("APPLICATION_PATH", realpath(DIR(__FILE__) . '/../app'));
Now this Global variable "APPLICATION_PATH" can be used to include all the files instead of calling realpath() everytime you include a new file.
现在可以使用全局变量“APPLICATION_PATH”包括所有的文件,而不是调用realpath()每次你包括一个新文件。
EX:
例:
include(APPLICATION_PATH ."/config/config.ini";
Hope it helps ;-)
希望它帮助;-)
#1
140
This should work
这应该工作
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/inc/include1.php";
Edit: added imporvement by aussieviking
编辑:添加了奥斯西维克的进项
#2
41
You can use relative paths. Try __FILE__
. This is a PHP constant which always returns the path/filename of the script it is in. So, in soap.php
, you could do:
可以使用相对路径。__FILE__试试。这是一个PHP常量,它总是返回它所在脚本的路径/文件名。所以,在肥皂。php,你能做的:
include dirname(__FILE__).'/../inc/include.php';
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2,
__FILE__
always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances. (source)文件的完整路径和文件名。如果在包含中使用,则返回包含文件的名称。由于PHP 4.0.2, __FILE__总是包含一个解析符号链接的绝对路径,而在旧版本中,它在某些情况下包含相对路径。(源)
Another solution would be to set an include path in your httpd.conf or an .htaccess file.
另一种解决方案是在httpd中设置包含路径。conf或.htaccess文件。
#3
7
have a look at http://au.php.net/reserved.variables
查看http://au.php.net/reserved.variables
I think the variable you are looking for is: $_SERVER["DOCUMENT_ROOT"]
我认为您正在寻找的变量是:$_SERVER["DOCUMENT_ROOT"]
#4
6
You could define a constant with the path to the root directory of your project, and then put that at the beginning of the path.
您可以使用项目的根目录的路径定义一个常量,然后将其放在路径的开头。
#5
4
Another way to handle this that removes any need for includes at all is to use the autoload feature. Including everything your script needs "Just in Case" can impede performance. If your includes are all class or interface definitions, and you want to load them only when needed, you can overload the __autoload()
function with your own code to find the appropriate class file and load it only when it's called. Here is the example from the manual:
另一种处理此问题的方法是使用autoload特性,这样就完全不需要include了。包括脚本需要的一切“以防万一”可能会影响性能。如果您的include是所有类或接口定义,并且您只希望在需要时加载它们,那么您可以使用您自己的代码重载__autoload()函数,以找到适当的类文件,并只在调用它时加载它。下面是手册中的例子:
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
As long as you set your include_path variables accordingly, you never need to include a class file again.
只要您相应地设置include_path变量,就永远不需要再包含类文件。
#6
2
Another option, related to Kevin's, is use __FILE__
, but instead replace the php file name from within it:
另一个与Kevin相关的选项是使用__FILE__,但是要从其中替换php文件名:
<?php
$docRoot = str_replace($_SERVER['SCRIPT_NAME'], '', __FILE__);
require_once($docRoot . '/lib/include.php');
?>
I've been using this for a while. The only problem is sometimes you don't have $_SERVER['SCRIPT_NAME']
, but sometimes there is another variable similar.
我用这个已经有一段时间了。唯一的问题是,有时您没有$_SERVER['SCRIPT_NAME'],但有时还有另一个类似的变量。
#7
2
I found this to work very well!
我发现这个很有效!
function findRoot() {
return(substr($_SERVER["SCRIPT_FILENAME"], 0, (stripos($_SERVER["SCRIPT_FILENAME"], $_SERVER["SCRIPT_NAME"])+1)));
}
Use:
使用:
<?php
function findRoot() {
return(substr($_SERVER["SCRIPT_FILENAME"], 0, (stripos($_SERVER["SCRIPT_FILENAME"], $_SERVER["SCRIPT_NAME"])+1)));
}
include(findRoot() . 'Post.php');
$posts = getPosts(findRoot() . 'posts_content');
include(findRoot() . 'includes/head.php');
for ($i=(sizeof($posts)-1); 0 <= $i; $i--) {
$posts[$i]->displayArticle();
}
include(findRoot() . 'includes/footer.php');
?>
#8
1
I think the best way is to put your includes in your PHP include path. There are various ways to do this depending on your setup.
我认为最好的方法是在PHP include路径中加入include。根据您的设置,有多种方法可以实现这一点。
Then you can simply refer to
然后你可以参考
require_once 'inc1.php';
from inside any file regardless of where it is whether in your includes or in your web accessible files, or any level of nested subdirectories.
从任何文件的内部,无论它在您的包含中还是在您的web可访问文件中,或任何级别的嵌套子目录中。
This allows you to have your include files outside the web server root, which is a best practice.
这允许您在web服务器根之外拥有您的包含文件,这是一种最佳实践。
e.g.
如。
site directory
html (web root)
your web-accessible files
includes
your include files
Also, check out __autoload for lazy loading of class files
另外,请查看__autoload,以延迟加载类文件。
http://www.google.com/search?q=setting+php+include+path
http://www.google.com/search?q=setting + php +有+路径
http://www.google.com/search?q=__autoload
http://www.google.com/search?q=__autoload
#9
1
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");
I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.
我使用这一行代码。它返回站点树的“顶部”,然后返回所需的文件。
For example, let's say i have this file tree:
例如,假设我有这个文件树:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.phpdomain.com/aaa/index.php domain.com/bbb/ccc/ddd/index.php domain.com/_resources/functions.php
I can include the functions.php file from wherever i am, just by copy pasting
我可以包含函数。php文件,无论我在哪里,只是复制粘贴。
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
If you need to use this code many times, you may create a function that returns the str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))
part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named path_back
):
如果需要多次使用此代码,可以创建一个函数,返回str_repeat('../'、(substr_count(getenv('SCRIPT_URL')、'/')-1)部分。然后在包含的第一个文件中插入这个函数。我有一个“初始化。php“文件,我在每个php页面的最顶端包含这个函数。下次我需要包含文件时,我实际上只是使用了函数(名为path_back):
require(path_back()."/_resources/another_php_file.php");
#10
0
@Flubba, does this allow me to have folders inside my include directory? flat include directories give me nightmares. as the whole objects directory should be in the inc directory.
@Flubba,这是否允许我的include目录中包含文件夹?扁平目录给我带来噩梦。因为整个对象目录应该在inc目录中。
Oh yes, absolutely. So for example, we use a single layer of subfolders, generally:
哦,是的,当然。例如,我们使用一层子文件夹,通常是:
require_once('library/string.class.php')
You need to be careful with relying on the include path too much in really high traffic sites, because php has to hunt through the current directory and then all the directories on the include path in order to see if your file is there and this can slow things up if you're getting hammered.
你需要小心依靠包括路径太多真的高流量的网站,因为php亨特通过当前目录,然后所有的目录包括路径为了看你的文件,这可以缓慢的东西如果你得到了。
So for example if you're doing MVC, you'd put the path to your application directoy in the include path and then specify refer to things in the form
举个例子,如果你在做MVC,你会把你的应用程序directoy的路径放在include路径中然后指定引用表单中的东西
'model/user.class'
'controllers/front.php'
or whatever.
之类的。
But generally speaking, it just lets you work with really short paths in your PHP that will work from anywhere and it's a lot easier to read than all that realpath document root malarkey.
但是一般来说,它只是让你使用短路径在PHP将从任何地方访问,而且它比这一切更容易读realpath文档根胡说。
The benefit of those script-based alternatives others have suggested is they work anywhere, even on shared boxes; setting the include path requires a little more thought and effort but as I mentioned lets you start using __autoload which just the coolest.
其他人提出的基于脚本的替代方案的好处是,它们可以在任何地方工作,甚至可以在共享框上工作;设置包含路径需要更多的思考和努力,但是正如我提到的,允许您开始使用__autoload,这是最酷的。
#11
0
If you are going to include specific path in most of the files in your application, create a Global variable to your root folder.
如果要在应用程序中的大多数文件中包含特定路径,请为根文件夹创建一个全局变量。
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../app'));
or
define("APPLICATION_PATH", realpath(DIR(__FILE__) . '/../app'));
Now this Global variable "APPLICATION_PATH" can be used to include all the files instead of calling realpath() everytime you include a new file.
现在可以使用全局变量“APPLICATION_PATH”包括所有的文件,而不是调用realpath()每次你包括一个新文件。
EX:
例:
include(APPLICATION_PATH ."/config/config.ini";
Hope it helps ;-)
希望它帮助;-)