【发布时间】:2017-10-24 03:03:35
【问题描述】:
我无法理解 PHP 选择如何编译或在文件中包含文件的工作原理。
我需要在head.php 中添加secrets.php。
我需要在index.php 中使用head.php。
head.php
require "../environment/secrets.php";
index.php
require "php/head.php";
我得到一个错误:
警告:require_once(../environment/secrets.php):打开失败 流:/home/ubuntu/workspace/Cally 中没有这样的文件或目录 Dai/php/head.php 在第 2 行调用堆栈:0.0002 234760 1. {main}() /home/ubuntu/workspace/Cally Dai/index.php:0 0.0007 236632 2. require_once('/home/ubuntu/workspace/Cally Dai/php/head.php') /home/ubuntu/workspace/Cally Dai/index.php:5 致命错误: require_once():打开失败需要'../environment/secrets.php' (include_path='.:/usr/share/php:/usr/share/pear') 在 /home/ubuntu/workspace/Cally Dai/php/head.php 在第 2 行调用堆栈: 0.0002 234760 1. {main}() /home/ubuntu/workspace/Cally Dai/index.php:0 0.0007 236632 2. require_once('/home/ubuntu/workspace/Cally Dai/php/head.php') /home /ubuntu/workspace/Cally Dai/index.php:5
我哪里错了?
【问题讨论】:
-
这可能是一个长镜头,但你是否已经尝试过设置 require "../environment/secrets.php";相对于 index.php?值得一试。需要“环境/secrets.php”;
-
@TimothyBomer 这使根目录中的所有内容都可以正常工作,但其他文件(例如项目文件夹中的文件)现在开始失败。相对于请求所需文件的文件,似乎需要该文件……但这不应该是正确的行为吗?
-
尝试改变:require "../environment/secrets.php";要求 (realpath(dirname(__FILE__)."/environment/secrets.php"));