猿问

警告:require_once(./mailer/class.phpmailer.php):

树:

--myproject

----mailer

-------class.phpmailer.php

----test

-------index.php

----site.php

----class.php

----db.php

----index.php

index.php:(两个)

<?php 

require_once '../site.php';

?>

网站.php:

<?php

require_once "class.php";

?>

类.php

<?php

require_once 'db.php';

require_once('./mailer/class.phpmailer.php');

?>

当我访问测试时,它显示:


警告:require_once(./mailer/class.phpmailer.php):无法打开流:第 3 行 C:\wamp\www\myproject\class.php 中没有这样的文件或目录


致命错误:require_once(): Failed opening required './mailer/class.phpmailer.php' (include_path='.;C:\php\pear') in C:\wamp\www\myproject\class.php on line 3


我也试过,include_once但同样的错误!


拉丁的传说
浏览 186回答 3
3回答

慕尼黑的夜晚无繁华

使用$_SERVER['DOCUMENT_ROOT']并包含完整路径。例如,index.php您可以在两个文件上执行以下操作:require_once($_SERVER['DOCUMENT_ROOT'].'/site.php');并在&nbsp;site.phprequire_once($_SERVER['DOCUMENT_ROOT'].'/class.php');并在&nbsp;class.phprequire_once($_SERVER['DOCUMENT_ROOT'].'/db.php'); require_once($_SERVER['DOCUMENT_ROOT'].'/mailer/class.phpmailer.php');之所以?因为$_SERVER['DOCUMENT_ROOT']会动态返回服务器上的完整文件路径到文档根目录。这就像说(假设您的文档根目录是/www/username/public_html):require_once('/www/username/public_html/db.php');这将始终相同 - 直到您更改到新的目录位置或服务器。发生这种情况时,PHP 将为您完成工作,而不是重写每个require_once(),因为您已经使用过$_SERVER['DOCUMENT_ROOT']。与绝对路径相比,使用相对路径可能会令人困惑,因为它是相对于当前位置的。

MMTTMM

让我试着回答这个问题。在文件夹 test 下的文件 index.php 上,您可以使用如下代码:<?php&nbsp;require_once '../site.php';?>在 site.php 上,您可以使用如下代码:<?phprequire_once "class.php";?>在 class.php 上,您可以添加如下代码:<?phprequire_once 'db.php';require_once('mailer/class.phpmailer.php');?>在文件夹 myproject 根目录下的文件 index.php 上,您可以使用如下代码:<?php&nbsp;require_once 'site.php';?>我希望这个技巧可以帮助你。

PIPIONE

你打电话给require_once('./mailer/class.phpmailer.php');从index.php里面,所以这个文件在上面两层。改变require_once('./mailer/class.phpmailer.php');到require_once('../mailer/class.phpmailer.php');更新刚刚意识到你需要两个 index.php 文件工作。这不是最漂亮的解决方案,但您可以这样做:/test/index.php<?php$nested = true;require_once '../site.php';?>/index.php<?php&nbsp;$nested = false;require_once 'site.php';?>这个新的“嵌套”变量将确定文件是在目录内还是在基础上。编辑你的class.php看起来像这样:<?phprequire_once 'db.php';if ($nested == true)require_once('../mailer/class.phpmailer.php');elseif ($nested == false)require_once('./mailer/class.phpmailer.php');?>这应该可以解决两个文件中的问题。
随时随地看视频慕课网APP
我要回答