为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
拉丁的传说 2021-11-05 15:10:28
树:--myproject----mailer-------class.phpmailer.php----test-------index.php----site.php----class.php----db.php----index.phpindex.php:(两个)<?php require_once '../site.php';?>网站.php:<?phprequire_once "class.php";?>类.php<?phprequire_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但同样的错误!
查看完整描述

3 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

使用$_SERVER['DOCUMENT_ROOT']并包含完整路径。

例如,index.php您可以在两个文件上执行以下操作:

require_once($_SERVER['DOCUMENT_ROOT'].'/site.php');

并在 site.php

require_once($_SERVER['DOCUMENT_ROOT'].'/class.php');

并在 class.php

require_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']

与绝对路径相比,使用相对路径可能会令人困惑,因为它是相对于当前位置的。


查看完整回答
反对 回复 2021-11-05
?
MMTTMM

TA贡献1869条经验 获得超4个赞

让我试着回答这个问题。


在文件夹 test 下的文件 index.php 上,您可以使用如下代码:


<?php 

require_once '../site.php';

?>

在 site.php 上,您可以使用如下代码:


<?php

require_once "class.php";

?>

在 class.php 上,您可以添加如下代码:


<?php

require_once 'db.php';

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

?>

在文件夹 myproject 根目录下的文件 index.php 上,您可以使用如下代码:


<?php 

require_once 'site.php';

?>

我希望这个技巧可以帮助你。


查看完整回答
反对 回复 2021-11-05
?
PIPIONE

TA贡献1829条经验 获得超9个赞

你打电话给


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 


$nested = false;

require_once 'site.php';


?>

这个新的“嵌套”变量将确定文件是在目录内还是在基础上。


编辑你的class.php看起来像这样:


<?php


require_once 'db.php';


if ($nested == true)

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


elseif ($nested == false)

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


?>

这应该可以解决两个文件中的问题。


查看完整回答
反对 回复 2021-11-05
  • 3 回答
  • 0 关注
  • 185 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信