gpt4 book ai didi

php - require_once 没有像我期望的那样工作

转载 作者:行者123 更新时间:2023-12-04 05:14:49 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




8年前关闭。




Possible Duplicate:
relative path in require_once doesn’t work



我有一个这样的项目结构:
ProjectName/
src
test
TestClass.php
tests
TestTestClass.php

当我尝试做 require_once '/ProjectName/src/test/TestClass.php';进入 tests/TestTestClass.php 我收到来自 PHP 的错误说明: 没有这样的文件或目录。

我检查了拼写,我检查了一切。我不能给它完整的 /home/userName/bla/bla路径,因为我需要将其交给其他人。

有什么想法吗?

最佳答案

这是预期的行为。当你执行 TestTestClass.php ,您的工作目录设置为 ProjectName/tests .

最好用../src/test/TestClass.php .

您的路径实际上是绝对的,因此您将直接从 / 开始工作。 ,这不是您所期望的。如果您的 include_path设置为您服务器的根目录,那么您的代码将在没有第一个 / 的情况下工作.如果您不想依赖 include_path或任意级别(使用 .. ),您始终可以在定义应用程序根目录完整路径的第一个文件中设置环境变量,然后将其用于所有包含。

例如,/ProjectName/index.php :

define('APPPATH', __DIR__);

.. 并在 /ProjectName/tests/TestTestClass.php :
require_once APPPATH . '/src/test/TestClass.php';

关于php - require_once 没有像我期望的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14419434/

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