为什么我不能包含这个PHP文件?

时间:2021-02-04 09:57:33

I have two PHP files in a directory. One includes the other, <?php include 'channels.phtml'; ?>, and that works fine. The problem arises when I move the included file to the parent directory and adjust the call, <?php include '../channels.phtml'; ?>. In this case, the file is not found.

我在目录中有两个PHP文件。一个包括另一个,<?php include'channel.phtml'; ?>,这很好。当我将包含的文件移动到父目录并调整调用时,问题就出现了,<?php include'../channels.phtml'; ?>。在这种情况下,找不到该文件。

Why is it so?

为什么会这样?

I am using Ubuntu and I don't think it's a permission problem, but here's the details anyhow:

我正在使用Ubuntu,我不认为这是一个权限问题,但无论如何这里是详细信息:

parent directory: drwxrwxr-x 6 kim kim 4096 Mar 26 08:42 org

父目录:drwxrwxr-x 6 kim kim 4096 Mar 26 08:42 org

child directory: drwxrwxr-x 2 kim kim 4096 Mar 26 08:47 profile

子目录:drwxrwxr-x 2 kim kim 4096 Mar 26 08:47 profile

included file: -rwxrwxrwx 1 kim kim 28 Mar 26 08:42 channels.phtml

包含文件:-rwxrwxrwx 1 kim kim 28 Mar 26 08:42 channels.phtml

I tried require, with the same result.

我试过要求,结果相同。

EDIT:

编辑:

The fix put forward by mesutozer does indeed work, but it should not be required. From the PHP manual entry on the include command:

mesutozer提出的修复确实有效,但不应该要求它。从include命令的PHP手册条目:

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

如果定义了一个路径 - 无论是绝对路径(从Windows上的驱动器号或者\或Unix / Linux系统上的/开始)还是相对于当前目录(以。或..开头) - include_path将被完全忽略。例如,如果文件名以../开头,则解析器将查找父目录以查找所请求的文件。

Note that phpinfo shows that he current directory (.) is on the include path, and as far as I am aware I have not disabled relative paths.

请注意,phpinfo显示当前目录(。)位于包含路径上,据我所知,我没有禁用相对路径。

FYI, I am using a ZF2.

仅供参考,我使用的是ZF2。

1 个解决方案

#1


1  

You need to have current folder in your include path, or specify other file's full path as:

您需要在包含路径中包含当前文件夹,或将其他文件的完整路径指定为:

<?php include dirname(__FILE__) . '/../channels.phtml'; ?>

#1


1  

You need to have current folder in your include path, or specify other file's full path as:

您需要在包含路径中包含当前文件夹,或将其他文件的完整路径指定为:

<?php include dirname(__FILE__) . '/../channels.phtml'; ?>