【发布时间】:2015-09-30 11:20:05
【问题描述】:
我试图要求一些文件,但它一直告诉我找不到它们。这些文件位于不同的文件夹中。
index.php -> ../core/init.php -> ../library/autoload.php
谁能帮我解决这个问题?在过去的一个小时里,这让我发疯了。
【问题讨论】:
标签: php require-once
我试图要求一些文件,但它一直告诉我找不到它们。这些文件位于不同的文件夹中。
index.php -> ../core/init.php -> ../library/autoload.php
谁能帮我解决这个问题?在过去的一个小时里,这让我发疯了。
【问题讨论】:
标签: php require-once
如果您尝试包含或需要文件,则路径必须来自完整根目录。试试这个:
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
// This is where index.php probably is
require_once($root.'sanitize.php');
require_once($root.'general.php');
?>
如果您回显$root,您将看到文件的实际路径。
【讨论】:
$root.'/folder/libaries/general.php'。如果你使用 ? $root.'general.php',require 将假定您包含的文件位于您的根文件夹中。您需要在两者之间添加文件夹。
不,您不需要 require 脚本的完整路径。
问题在于,在您的 autoload.php 中,它位于 ../library/ 文件夹中,当您尝试“require_once”时,路径从 ../library/ 开始
“require”在调用函数的主脚本文件夹中开始。
/somefolder/index.php
require('../core/init.php');
/core/init.php
require('../library/autoload.php');
/library/autoload.php
print "hello";
【讨论】:
并让你的路径相对。
对于 PHP
$rootpath=dirname(__FILE__);
require_once($rootpath.'sanitize.php');
require_once($rootpath.'general.php');
【讨论】: