gpt4 book ai didi

PHP:require 无法正常工作

转载 作者:行者123 更新时间:2023-12-04 16:37:40 25 4
gpt4 key购买 nike

我有一个目录根:

index.php
includes/
template.php
testfile.php
phpFiles/
processInput.php
testfile.php

索引.php:
require_once("includes/template.php");

模板.php:
require_once("includes/phpFiles/processInput.php")

进程输入.php:
require_once("testfile.php")
require_once("../testfile.php")

这段代码在运行index.php 时会起作用,当然在运行template.php 时就不起作用了。

如您所见,index.php 像往常一样包含 template.php。但是在 template.php 中,您必须像在 index.php 所在的目录中一样包含。 但是,在 processInput.php 中,您必须像在 processInput.php 所在的目录中一样包含。

为什么会发生这种情况,我该如何修复它以便包含路径始终是完成 require 的文件的目录?第二个包含的文件与请求的文件具有相同的包含路径,但下一个没有。

谢谢你的帮助!

编辑:奇怪的是我在类文件夹中包含了类。并且它包含了它应该包含的其他文件,即使路径是相对的。为什么会发生这种情况,我该如何解决?

非常重要的编辑:我刚刚意识到这一切都是因为在我的示例中,includes/phpFiles/processInput.php 中的包含在同一目录中包含一个文件: require_once("file in same dir.php");这就是原因。如果您要包含一个文件而没有指定比文件名更多的内容,那么 include_path 实际上是写入 require 的文件所在的目录。有人可以确认这一点吗?

最佳答案

使用绝对路径。

require_once($_SERVER['DOCUMENT_ROOT']."/includes/phpFiles/processInput.php");

为您的所有 require 使用类似的表格d 文件,无论您身在何处,它们都可以使用。

关于PHP:require 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10870905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com