1

My file structure:

  • sys : a folder containing 3 sub folders: inc, class, and config. The inc folder has a php file init.php.
  • index.php in the root folder.

My config folder has a file with the DB credentials which is include_once ('..\config\db_credentials.php'); in init.php. The init file is working fine with included file.

I have also included include_once ('sys\inc\init.inc.php'); in index.php but if I run the index file it says:

Warning: include_once(..\config\db_credentials.php): failed to open stream: No such file or directory in C:\wamp\www\example\sys\inc\init.inc.php on line 3

and

Warning: include_once(): Failed opening '..\config\db_credentials.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\example\sys\inc\init.inc.php on line 3

What am I doing wrong and how can I make this work?

1
  • can you please provide file/folder with a tree structure. Commented Jun 12, 2014 at 11:59

5 Answers 5

4

use slashes instead of backslashes

include_once ('../config/db_credentials.php');.
Sign up to request clarification or add additional context in comments.

Comments

2

I would recommend you set include path to the location of your files to be included, and use namespaces to separate between them.

Relative locations of your files are bad, such as ../foo.php, but using relative location based on include path (config/dbconfig.php) and only that you would not get any more surprises. This would result in a more robust configuration.

Comments

1

Make sure that the file you're including is relative to the current document.

Or as I would do it:

Use the $_SERVER['DOCUMENT_ROOT'] as a prefix everywhere (or define('DOCROOT', $path); in the config/index)

Comments

0

In your init file you have to put include_once ('config/db_credentials.php');.

Becausewhen you have cascading includes, the root directory is always the first called file. Here it's your index.php file ; so all includes have to be from here.

Your current include will work if your directly launch the init.php file. That's one of the cons against includes ; namespaces are better for that :)

Comments

-2

You can include file via file php built in function.

first echo file; in which file u want to include.

use dirname(file); to out form one directory.

when u come on your desire directory then include like

include( dirname(file).'/file.php' );

Note: file function return u absolute path of your file.

may be its helpful for u.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.