require_once with subfolders [closed]
Asked Answered
F

7

7

I have Folder Home with two subfolders like the following

+Home  
 +include  
   - membersite_config.php

+iDiscover  
   -index.php

in index.php I added the require_once script to access membersite_config.php

<?PHP

require_once($_SERVER['DOCUMENT_ROOT'].'/include/membersite_config.php');
?>

I get the following error : Warning: require_once(./include/fg_membersite.php): failed to open stream: No such file or directory in D:\Home\include\membersite_config.php on line 2
Fatal error: require_once(): Failed opening required './include/fg_membersite.php' (include_path='.;C:\php\pear') in D:\Home\include\membersite_config.php on line 2

The error says No such file or directory while the The path "D:\Home\include\membersite_config.php" is correct . When I move index.php to be under the root , the page works well.

I also tried to use the following code as described here but it gives the same error

<?PHP


define('ROOT', 'D:Home\\');
require_once(ROOT ."/include/membersite_config.php");
?>

Edit :

I too tried require_once("../include/membersite_config.php"); And it gives the same error

Thanks in advance

Forefather answered 24/2, 2013 at 7:22 Comment(3)
Have you checked the file permissions?Known
Try require_once("../include/membersite_config.php");Henryhenryetta
@Known yes, everything works well when I move index.php to the root.Forefather
I
6

You must include like this

require_once(dirname(__FILE__) . '/include/membersite_config.php');

Because DocumentRoot can be not set in httpd.conf

Interlaminate answered 24/2, 2013 at 8:18 Comment(0)
L
1

The error means that you have a require in your membersite_config.php that does not work on line 2.

Leopard answered 24/2, 2013 at 7:42 Comment(3)
I can see that it doesn't work. It says failed to open stream: No such file or directory , while the directory do exists. That's why I am asking whats wrong in my codeForefather
Read what I typed. The error is not in the index.PHP.Leopard
index.php can't access membersite_config.php , when it's inside the subfolder iDiscover. When I move index.php to the root. the same code works with no problems.Forefather
N
1
require_once(dirname(dirname(__FILE__)).'/home/include/membersite_config.php');
Numerable answered 24/2, 2013 at 8:17 Comment(0)
H
0

Try this:

require_once("../include/membersite_config.php");

don't forget to check the file/folder permissions!

Henryhenryetta answered 24/2, 2013 at 7:25 Comment(1)
Thanks. I tried it but it gives the same error . The file folder permission is full control. And I can access the file when I move index.php to the root instead of putting it inside iDiscover folderForefather
A
0

Try this:

<?PHP
require_once('../include/membersite_config.php');
?>
Amateurism answered 24/2, 2013 at 7:26 Comment(5)
Thanks. But it gives the same errorForefather
Are you placing the codes here?: iDiscover/index.phpAmateurism
yes . iDiscover is subfolder under the root (Home) folderForefather
Please confirm the path: home/iDiscover/index.php or iDiscover/index.phpAmateurism
The physical path is home/iDiscover/index.php and membersite_config path is home/include/membersite_config.phpForefather
C
0

'..' means back

require_once('../include/membersite_config.php');

means get out the folder you are in, then enter include...

if you have to get out three folders do this ../../../include... etc.

Coarse answered 24/2, 2013 at 7:39 Comment(0)
G
0

For debugging try this:

<?php
$dir_files=glob($_SERVER['DOCUMENT_ROOT'].'/include/*');
print_r($dir_files);
?>

after you run this script, you will see if your filename(s) are correct.

Gliadin answered 24/2, 2013 at 8:23 Comment(5)
I tried it and the file do existForefather
and i suppose is is spelled exactly like membersite_config.php?Gliadin
If you run membersite_config.php, you get any error? (like Guido Jansen suggest)Gliadin
membersite_config.php works. and the same code works well when I move indexx.php to the root folder instead of iDiscover folderForefather
did you tried include()?Gliadin

© 2022 - 2024 — McMap. All rights reserved.