我想找到一种方法,使include_once();
路径从开始目录开始,然后找到路径,例如而不是../../../path/to/file
我会有/path/to/file
。如果我不这样做/path/to/file
它说没有这样的文件或目录,这里是我的直接代码php include_once path
<?php
include_once("/assets/page_assets.php");
?>
我想找到一种方法,使include_once();
路径从开始目录开始,然后找到路径,例如而不是../../../path/to/file
我会有/path/to/file
。如果我不这样做/path/to/file
它说没有这样的文件或目录,这里是我的直接代码php include_once path
<?php
include_once("/assets/page_assets.php");
?>
如果你有一个Apache服务器
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/assets/page_assets.php");
?>
"/assets/page_assets.php"
意味着from the root of the disk
,不从的根文件夹服务器。如果您正在讨论其他一些beginning directory
,则将其作为单独的常量/变量定义为物理路径(服务器文件系统中的路径,而不是Web路径),并将其前置到包含的路径中,如上所示。
与chdir($_SERVER['DOCUMENT_ROOT']);
启动脚本现在你可以调用include("path/to/file.php");
,它将开始从根目录看。
要做到这一点,你必须使用根路径。在某些情况下,这可能看起来像/var/www/mysite.com/assets/page_assets.php
。找到这条路径的一种方法是使用__FILE__
(这是2在前面和后面都强调)。如果你从一个文件回应,它会告诉你完整的路径。您应该可以使用它来设置正确的完整路径。
您可以指定包括路径明确:
<?php ini_set('include_path',ini_get('include_path').':../../../:'); ?>
但作为爽快的评论中提到,你必须包括你的文件,而不斜线:从一开始就从directory`
<?php
include_once("assets/page_assets.php");
?>
'路径开始网络服务器的根? '/ path/to/file'是文件系统根目录的路径! – Cheery 2012-01-31 20:40:09