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 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加