1

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]
shibbir ahmed
  • 1,014
  • 2
  • 18
  • 34

1 Answers1

3

Try using:

__DIR__

Infront of the import inside header.php file.

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

See Magic Constants

Janno
  • 542
  • 4
  • 15
  • Showing error message : `Warning: require_once(D:\Softwares Installed\xampp-old\htdocs\aponit\dev../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` – shibbir ahmed Jul 28 '16 at 07:20
  • `Fatal error: require_once(): Failed opening required 'D:\Softwares Installed\xampp-old\htdocs\aponit\dev../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` – shibbir ahmed Jul 28 '16 at 07:20
  • Put / there as well, modified my answer. – Janno Jul 28 '16 at 07:20
  • well it's working fine. Now I `echo` the code and it's output is : `D:\Softwares Installed\xampp-old\htdocs\aponit\dev/../config/config.php` – shibbir ahmed Jul 28 '16 at 07:23
  • My question is there is a dot `/../` ! then how it's finding the `config.php` file ? – shibbir ahmed Jul 28 '16 at 07:24
  • I don't get what you meant. If you mean /../, that just tells PHP to go back one folder. If you mean inside the require_once, the . puts the 2 strings (_ _DIR_ _ and '/../config/config.php') togheter. – Janno Jul 28 '16 at 07:26
  • Great Answer! But I found a not-found error message when click on this link from index.php page from zones folder : `" >Edit`. Should I need to edit my .httaccess rules ? – shibbir ahmed Jul 28 '16 at 07:29
  • Check that the SITE_URL is right and has a trailing slash in the end. I think it might not have a trailing slash. Try using echo SITE_URL . "/zones/update-zone" .. etc – Janno Jul 28 '16 at 07:31