dongyuan3094 2016-07-28 07:13
浏览 104
已采纳

为什么require_once它不工作如果我从不同的文件夹调用相同的文件?

Here is my root directory

dev (root folder)     
|   zones(folder)
|   |    index.php
|   |    new-zone.php
|   |    update-zone.php
index.php
login.php
signup.php
header.php
functions.php

outside of root folder I have config file called config.php

Now from header.php page I am calling following code and it's working fine:

require_once('../config/config.php');
require_once('functions.php');

But now I need to call same header.php file from the folder zones/index.php page and it's code is bellow :

require_once("../header.php"); 

But showing following error message :

require_once(../config/config.php): failed to open stream: No such file or directory in D:\Softwares Installed\xampp-old\htdocs\aponit\dev\header.php on line 3

Fatal error: require_once(): Failed opening required '../config/config.php' (include_path='.;D:\Softwares Installed\xampp-old\php\PEAR\;D:\xampp\htdocs\smarty\libs\') in D:\Softwares Installed\xampp-old\htdocs\aponit\dev\header.php on line 3

How can I solve this error ? I want to call header.php file from different folders ?

Note : I am using following .htaccess rules to use SEO friendly url and custom error message page.

.htaccess code :

Options -MultiViews
ErrorDocument 404 http://localhost/aponit/dev/not-found.php
ErrorDocument 500 http://localhost/aponit/dev/404.php
RewriteEngine on

RewriteRule ^(?:zones/)?update-zone/(\w+)/?$ update-zone.php?z=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
  • 写回答

1条回答 默认 最新

  • duancuan7057 2016-07-28 07:17
    关注

    Try using:

    __DIR__
    

    Infront of the import inside header.php file.

    require_once(__DIR__ . '/../config/config.php');
    

    See Magic Constants

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大