php 包含来自父目录或其他目录的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8826567/
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
Include files from parent or other directory
提问by Darren
I'm needing to include a file from the parent directory, and other sub-directories, into a sub-directory. I've done it before by simply using include('/rootdirectory/file.php'); but now it won't seem to work.
我需要将父目录和其他子目录中的文件包含到子目录中。我以前通过简单地使用 include('/rootdirectory/file.php'); 来完成它。但现在它似乎不起作用。
Just wondering how I can do this, thanks.
只是想知道我怎么能做到这一点,谢谢。
Here's my exact line:
这是我的确切线路:
include('/forums/groups.php');
It's giving me this error(the page still runs):
它给了我这个错误(页面仍在运行):
Warning:include(/forums/groups.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\forums\blog\posts.php on line
Warning:include() [function.include]: Failed opening '/forums/groups.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\forums\blog\posts.php on line 3
警告:include(/forums/groups.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\forums\blog\posts.php on line
警告:include() [function.include]:在 C:\xampp\htdocs\forums\blog 中打开“/forums/groups.php”以包含(include_path='.;C:\xampp\php\PEAR')失败\posts.php 第 3 行
回答by Michael Berkowski
include()
and its relatives take filesystem paths, not web paths relative to the document root. To get the parent directory, use ../
include()
及其亲属采用文件系统路径,而不是相对于文档根目录的网络路径。要获取父目录,请使用../
include('../somefilein_parent.php');
include('../../somefile_2levels_up.php');
If you begin with a /
, an absolute system file path will be used:
如果以 a 开头,/
将使用绝对系统文件路径:
// Full absolute path...
include('/home/username/sites/project/include/config.php');
回答by Steven Spungin
If your server is not resolving the file from the parent directory using
如果您的服务器没有使用父目录解析文件
include '../somefilein_parent.php'
include '../somefilein_parent.php'
try this (using the parent directory relative to the script):
试试这个(使用相对于脚本的父目录):
include __DIR__ . "/../somefilein_parent.php";
include __DIR__ . "/../somefilein_parent.php";
回答by Frank
Here's something I wrote with that problem in mind:
考虑到这个问题,我写了一些东西:
<?
function absolute_include($file)
{
/*
$file is the file url relative to the root of your site.
Yourdomain.com/folder/file.inc would be passed as
"folder/file.inc"
*/
$folder_depth = substr_count($_SERVER["PHP_SELF"] , "/");
if($folder_depth == false)
$folder_depth = 1;
include(str_repeat("../", $folder_depth - 1) . $file);
}
?>
hope it helps.
希望能帮助到你。
回答by Christian Bekker
Depends on where the file you are trying to include from is located.
取决于您尝试包含的文件所在的位置。
Example:
例子:
/rootdir/pages/file.php
/rootdir/pages/file.php
/someotherDir/index.php
/someotherDir/index.php
If you wrote the following in index.php:
include('/rootdir/pages/file.php');
it would error becuase it would try to get:
如果您在 index.php: 中编写了以下内容,
include('/rootdir/pages/file.php');
则会出错,因为它会尝试获取:
/someotherDir/rootdir/pages/file.php
Which of course doesn't exist...
/someotherDir/rootdir/pages/file.php
哪个当然不存在...
So you would have to use include('../rootdir/pages/file.php');
所以你必须使用 include('../rootdir/pages/file.php');
回答by BobD
In laymans terms, and practicality, I see this as an old DOS trick/thing. Whoa! What was that? DOS? Never heard of it!
用外行的术语和实用性来说,我认为这是一个古老的 DOS 技巧/东西。哇!那是什么?操作系统?从来没有听说过!
".."backs you out of the current sub-directory one time to a higher folder/directory, and .. entertyped twice backs you out too 2 higher parent folders. Keep adding the ".. enter" back to back and you will soon find yourself at the top level of the directory.
“..”将您一次从当前子目录退出到更高的文件夹/目录,并且.. 输入两次也会将您退出 2 个更高的父文件夹。继续添加“ .. enter”,您很快就会发现自己位于目录的顶层。
As for Newbies to understand this better, consider this (in terms of the home PC or "C:\ drive" if you know what that means, rather than the web-servers/host "root directory" ). While your at it, Consider your website existing somewhere on your home PC's hard drive, buried in some folder under the C:\ drive. Lastly, you can think of it as ".." is back one directory and "/" is forward one directory/folder.
至于新手要更好地理解这一点,请考虑这一点(如果您知道这意味着什么,则使用家用 PC 或“C:\ 驱动器”,而不是网络服务器/主机“根目录”)。当你在做的时候,考虑你的网站存在于你的家用电脑硬盘上的某个地方,埋在 C:\ 驱动器下的某个文件夹中。最后,您可以将其视为“..”后退一个目录,“/”前进一个目录/文件夹。
Now! If you are using the command prompt and are within the "myDocuments" folder of your PC you must back out of that folder to get closer to the higher directory "C:\" by typing the "../". If you wanted to access a file that is located in the widows directory while you are still in the myDocuments folder you would theoretically type ../windows; in reality of DOS command prompt you would simply type .., but I am setting you up for the web. The /redirects forward to another directory naturally.
现在!如果您正在使用命令提示符并且位于PC的“ myDocuments”文件夹中,则必须退出该文件夹以通过键入“ ../”来接近更高的目录“C:\ ”。如果您想访问位于 widows 目录中的文件而您仍在 myDocuments 文件夹中,则理论上您应该键入../windows;实际上,在 DOS 命令提示符下,您只需键入..,但我正在为您设置网络。该/向前重定向到另一个目录自然。
Using "myDocuments" lets pretend that you created 2 folders within it called "PHP1" and "PHP2", in such we now have the folders:
使用“ myDocuments”让我们假设您在其中创建了 2 个名为“PHP1”和“PHP2”的文件夹,这样我们现在就有了文件夹:
- C:\myDocuments\PHP1
- C:\myDocuments\PHP2
- C:\我的文档\ PHP1
- C:\myDocuments\ PHP2
In PHP1 you place a file called index.php. and in PHP2 folder you placed a file called Found.php. it now becomes:
在 PHP1 中,您放置了一个名为 index.php 的文件。在 PHP2 文件夹中,您放置了一个名为 Found.php 的文件。现在变成:
- C:\myDocuments\PHP1\index.php
- C:\myDocuments\PHP2\found.php
- C:\myDocuments\PHP1\ index.php
- C:\myDocuments\PHP2\ found.php
Inside the C:\myDocuments\PHP1\index.phpfile you would need to edit and type something like:
在 C:\myDocuments\PHP1\index.php文件中,您需要编辑并键入以下内容:
<?php include ('../php2/found.php')?>
The ../is positional thus it considers your current file location "C:\myDocuments\PHP1\index.php" and is a directive telling it to back out of PHP1directory and enter or move forward into PHP2directory to look for the Found.phpfile. But does it read it? See my thoughts on trouble shooting below.
该../是位置因此它认为当前的文件位置“C:\我的文档\ PHP1 \的index.php”,是一个指令,告诉它背出来PHP1的目录,然后输入或前进到PHP2目录去寻找发现.php文件。但它读了吗?在下面查看我对故障排除的想法。
Now! suppose you have 1 folder PHP1 and a sub-folder PHP2:
现在!假设您有 1 个文件夹 PHP1 和一个子文件夹 PHP2:
- C:\myDocuments\PHP1\PHP2
- C:\myDocuments\ PHP1\PHP2
you would simply reference/code
你只需参考/代码
<?php include('/PHP2/found.php') ?>
as PHP2 exist as a sub-directory, below or within PHP1 directory.
因为 PHP2 作为子目录存在,位于 PHP1 目录之下或之内。
If the above does not work it may have something to do with access/htaccess or permission to the directory or a typo. To enhance this...getting into trouble shooting...If the "found.php" file has errors/typo's within it, it will crash upon rendering at the error, such could be the reason (require/require_once) that you are experiencing the illusion that it is not changing directories or accessing the file. At last thought on the matter, you may need to instantiate your functions or references in order to use the included/require "whatever" by creating a new variable or object such as
如果上述方法不起作用,则可能与 access/htaccess 或目录权限或错别字有关。为了增强这一点......进入故障排除......如果“found.php”文件中有错误/错字,它会在出现错误时崩溃,这可能是你的原因(require/require_once)体验它没有更改目录或访问文件的错觉。最后想到这个问题,您可能需要实例化您的函数或引用,以便通过创建一个新的变量或对象来使用包含/需要的“任何东西”,例如
$newObject = new nameobject("origianlThingy");
Remember, just because you are including/requiring something, sometimes means just that, it is included/required to run, but it might need to be recreated to make it active or access it. Newwill surely re-create an instance of it "if it is readable" and make it available within the current document while preserving the original. However you should reference the newly created variable $newObjectin all instances....if its global.
请记住,仅仅因为您包含/需要某些东西,有时意味着它被包含/需要运行,但可能需要重新创建以使其处于活动状态或访问它。New肯定会“如果它是可读的”重新创建它的一个实例,并使其在当前文档中可用,同时保留原始文档。但是,您应该在所有实例中引用新创建的变量$newObject....如果它是全局的。
To put this in perspective of some web host account; the web host is some whopping over sized hard-drive (like that on your PC) and your domain is nothing more than a folder they have assigned to you. Your folder is called the root. Inside that folder you can do anything you are allowed to do.
your "one of many ways" to move between directories/folders is to use the ../
however many times to back out of your current in referenceto folder position you want to find.
从某些网络主机帐户的角度来看;网络主机是一些超大尺寸的硬盘驱动器(如您的 PC 上的硬盘驱动器),而您的域只不过是他们分配给您的文件夹。您的文件夹称为根。在该文件夹中,您可以做任何您可以做的事情。你的“的途径之一,以目录之间移动” /文件夹是使用../
但多次回你目前的出参考您要查找的文件夹位置。
In my drunken state I realize that I know too much to be sane, and not enough to be insane!"
在我醉酒的状态下,我意识到我知道的太多,不能保持理智,还不够疯狂!”
回答by TimWolla
Any path beginning with a slash will be an absolute path. From the root-folder of the server and not the root-folder of your document root. You can use ../
to go into the parent directory.
任何以斜杠开头的路径都是绝对路径。来自服务器的根文件夹,而不是文档根目录的根文件夹。您可以使用../
进入父目录。
回答by CRAZYDATA
I took inspiration from frank and I added something like this in my "settings.php" file that is then included in all pages when there is a link:
我从弗兰克那里获得了灵感,并在我的“settings.php”文件中添加了这样的东西,然后当有链接时,它会包含在所有页面中:
"settings.php"
“设置.php”
$folder_depth = substr_count($_SERVER["PHP_SELF"] , "/");
$slash="";
for ($i=1;$i<=($folder_depth-2);++$i){
$slash= $slash."../";
}
in my header.php to be included in all pages:
在我的 header.php 中要包含在所有页面中:
a href= .... php echo $slash.'index.php'....
a href= .... php echo $slash.'index.php'....
seems it works both on local and hosted environment....
似乎它适用于本地和托管环境....
(NOTE: I am an absolute beginner )
(注意:我是一个绝对的初学者)
回答by Pradeep Kumar Kushwaha
Had same issue earlier solved like this :
之前有同样的问题是这样解决的:
include('/../includes/config.php'); //note '/' appearing before '../includes/config.php'
include('/../includes/config.php'); //注意'/'出现在'../includes/config.php'之前
回答by Eliter
the root directory (in PHP) is the directory of the file that is pinged. For example, I go to http://localhost/directory/to/file/index.php, the root directory will be "/dictory/to/file", since it's the one that you've made a web request for.
根目录(在 PHP 中)是被 ping 的文件的目录。例如,我转到http://localhost/directory/to/file/index.php,根目录将是“/dictory/to/file”,因为它是您向其发出 Web 请求的目录。
回答by Subrat Kumar Palhar
You may interest in using php's inbuilt function realpath(). and passing a constant DIR
您可能对使用 php 的内置函数 realpath() 感兴趣。并传递一个恒定的DIR
for example: $TargetDirectory = realpath(__DIR__."/../..");//Will take you 2 folder's back
例如: $TargetDirectory = realpath(__DIR__."/../.."); //将带你2个文件夹的回来
String realpath() :: Returns canonicalized absolute pathname ..
String realpath() :: 返回规范化的绝对路径名 ..