包括 php 中的文件

发布于 2024-11-02 11:52:51 字数 780 浏览 0 评论 0原文

嗨,
我尝试通过终端运行文件,但收到类似“包含路径不正确”的错误
例如,我在

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆那么伤 2024-11-09 11:52:51

我认为 include_once() 基本上将代码插入到您的文件中而不对其进行评估,因此该路径是相对于 include 文件(file.php,不
head.php)。

另外,我会对相对路径进行一些研究,因为您是从目录 /home/sekar/test/ 引用的,而不是文件的路径。

这可能有效:

file.php

<?php 
include_once "./includes/head.php";
?>

head.php

<?php 
include_once "../classes/cls.php";
?>

cls.php

<?php 
echo "this is from cls file";
?>

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, not
head.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

<?php 
include_once "./includes/head.php";
?>

head.php

<?php 
include_once "../classes/cls.php";
?>

cls.php

<?php 
echo "this is from cls file";
?>
羁客 2024-11-09 11:52:51

PHP include 与include_path 集合相关,其第一个元素是. 或当前工作目录。当前工作目录不必与 PHP 文件所在的目录相同,也不必与您的主目录(您似乎假设的)相同。有两种方法可以解决您的问题。

您可以通过将其添加到 file.php 的顶部来更改脚本的当前工作目录:

chdir(dirname(__FILE__));

或者,您可以将该目录添加到包含路径:

set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());

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:

chdir(dirname(__FILE__));

Or, you can add that directory to the include path:

set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
傲娇萝莉攻 2024-11-09 11:52:51

只需将其包含在

file.php

中,因为 file.php 和包含目录都位于当前(同一)目录(测试)中,您可以

<?php 
include_once "includes/head.php";
?>

在 head.php 中 包含 head.php 文件,如下所示head.php 和 cls.php 都出现在不同的目录中,您已包含该文件,如下所示。

head.php

<?php 
include_once "../classes/cls.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

<?php 
include_once "includes/head.php";
?>

here in head.php the head.php and cls.php both presented in different directories you have include the file as follows.

head.php

<?php 
include_once "../classes/cls.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 path classes/cls.php now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文