包括 php 中的文件
嗨,
我尝试通过终端运行文件,但收到类似“包含路径不正确”的错误
例如,我在
/home/sekar/test/file.php
php 文件的以下文件夹中有一个“test.php”,我在其中包含了一个文件“head.php”,
/home/sekar/test/includes/head.php
该 head.php 包含一个名为 cls.php 的类文件,该文件位于类文件夹中,
/home/sekar/test/classes/cls.php
我在终端中尝试了这样的操作,
php /home/sekar/test/file.php
为了清楚地看到这三个文件的内容,
file.php
<?php
include_once "./test/includes/head.php";
?>
head.php
<?php
include_once "./test/classes/cls.php";
?>
cls.php
<?php
echo "this is from cls file";
?>
任何人都可以帮助我解决这个问题吗?谢谢!
HI,
I try to run a file thru terminal but am getting the error like "include path is not correct"
for example, i have a "test.php" in following folder
/home/sekar/test/file.php
in file php i've included a file "head.php" which is in ,
/home/sekar/test/includes/head.php
Thes head.php includes a class file called cls.php which is in class folder,
/home/sekar/test/classes/cls.php
i tried like this in terminal,
php /home/sekar/test/file.php
for a clear a view just have a look @ contents of the those three files,
file.php
<?php
include_once "./test/includes/head.php";
?>
head.php
<?php
include_once "./test/classes/cls.php";
?>
cls.php
<?php
echo "this is from cls file";
?>
Can anyone help me to get around this issue? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为
include_once()
基本上将代码插入到您的文件中而不对其进行评估,因此该路径是相对于 include 文件(file.php,不head.php)。
另外,我会对相对路径进行一些研究,因为您是从目录
/home/sekar/test/
引用的,而不是文件的路径。这可能有效:
file.php
head.php
cls.php
I think that
include_once()
basically inserts code into your file without evaluating it, so the path is relative to that of the including file (file.php, nothead.php).
Also, I'd do a bit of research on relative paths, as you're referencing from the directory
/home/sekar/test/
, not the file's path.This might work:
file.php
head.php
cls.php
PHP include 与
include_path
集合相关,其第一个元素是.
或当前工作目录。当前工作目录不必与 PHP 文件所在的目录相同,也不必与您的主目录(您似乎假设的)相同。有两种方法可以解决您的问题。您可以通过将其添加到 file.php 的顶部来更改脚本的当前工作目录:
或者,您可以将该目录添加到包含路径:
PHP includes are relative to the set
include_path
, the first element of which is.
or the current working directory. The current working directory does not have to be the same as the directory your PHP file is in, and it does not have to be the same as your home directory (which you seem to be assuming). There are two ways to solve your problem.You can change the current working directory of your scripts by adding this to the top of file.php:
Or, you can add that directory to the include path:
只需将其包含在
file.php
中,因为 file.php 和包含目录都位于当前(同一)目录(测试)中,您可以
在 head.php 中 包含 head.php 文件,如下所示head.php 和 cls.php 都出现在不同的目录中,您已包含该文件,如下所示。
head.php
当您使用
../
时,它将从当前目录中出来,现在类和包含都显示在同一目录下,您可以包含路径现在classes/cls.php
。just include it as follows
file.php
as both file.php and includes directory is in the current(same) directory(test) you can include the head.php file as follows
here in head.php the head.php and cls.php both presented in different directories you have include the file as follows.
head.php
when you use
../
it will come out from current directory and now both the classess and includes presented under the same directory you can include the pathclasses/cls.php
now.