PHP include不能为现有文件工作


php include not working for existing files

我最近更改了我的自制框架,将模板文件存储在主题文件夹而不是常规模板文件夹中。现在的问题是,我已经这样做了,由于某种原因,我的include()指向正确的文件,但不工作。然而,由于某些原因,主题模板include()可以工作。

我收到这些错误:

Warning: require(lib/themes/original/templates/en/home/index.php): failed to open stream: No such file or directory in /home/stretch045/public_html/lib/theme/original/templateen.php on line 27
Warning: require(lib/themes/original/templates/en/home/index.php): failed to open stream: No such file or directory in /home/stretch045/public_html/lib/theme/original/templateen.php on line 27
Fatal error: require(): Failed opening required 'lib/themes/original/templates/en/home/index.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/stretch045/public_html/lib/theme/original/templateen.php on line 27

我相信最后一个可以解释它,但我不明白括号里的东西应该是什么意思,因为它不是我在共享服务器上的路径。

如果有必要,我会提供代码,但我不认为这可能是问题,因为路径是正确的

我认为问题是你试图包括文件夹themes的文件,当它可能应该是theme,至少这是我从你的错误中理解的:

Warning: require(lib/themes/original/templates/en/home/index.php): failed to open stream: No such file or directory in /home/stretch045/public_html/lib/theme/original/templateen.php on line 27

这里很明显,文件'/home/stretch045/public_html/lib/theme/original/templateen.php'试图包含文件'lib/themes/original/templates/en/home/index.php'.

如果您的框架的入口点是/home/stretch045/public_html/index.php,那么您使用相对路径的事实应该不是问题,因为include()的文档也适用于require()方法,因此它知道检查文件是否相对于当前工作目录存在,也相对于调用脚本的自己的目录

不工作,因为找不到文件。

在require语句中使用了相对路径。如果包含的文件使用相对路径包含其他文件(因为PHP使用相对于正在执行的脚本的路径,而不是包含include/require语句的文件的路径),这尤其有问题。

PHP尝试使用include_path查找相对引用文件,即.相对于执行的文件以及/usr/lib/php和/usr/local/lib/php(即/usr/lib/php/lib/themes/original/templates/en/home/index. PHP和/usr/local/lib/php/lib/themes/original/templates/en/home/index. PHP)。这些都不存在。

因此,使用正确的相对路径(例如,始终基于使用require(__DIR__.'/PATH');的当前脚本路径)或在require语句中使用绝对路径或将基本路径添加到include_path (:是路径分隔符)。