0
votes

i'm getting following error in my PHP file.

Warning: include(../config/config.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\my-proj\functions\function.php on line 2

let me describe my folder structure

  1. ROOT folder /index.php
  2. functions / function.php
  3. config / config.php
  4. signup / signup.php

now, If i use absolute path, then it is give the same error in signup.php, and if I use relative path then it is giving this error in index.php

any help would be appreciated.

3
if you know to ask question why don't you accept the answer? is it because you didn;t get any solution for your question? - Manigandan Arjunan

3 Answers

5
votes

use

include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
3
votes

The file paths are relative to the invoked script. If your application gets invoked by http requests to index.php, then the include() path needs to be relative to that - even if the include statement itself is located in the functions.php script.

A common workaround is to make all paths absolute in relation to the document root:

include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
// Note: leaving out array keys only valid in double quote string context.

That would work in index.php and functions.php alike.

0
votes

Use include __DIR__."/../config/config.php"; if you want to include a file relative to the file you're currently executing in. If you're using a version of php older than 5.3.0 (you shouldn't), replace __DIR__ with dirname(__FILE__).

$_SERVER['DOCUMENT_ROOT'] is not set when using commandline and requires that your project is relative to the DOCUMENT_ROOT instead of allowing the user to place it wherever they please. If phpMyAdmin used this variable, you would be forced to accommodate it instead of just placing it wherever you want. That's another thing, it's a variable, so there's a potential security issue too.

If config.php is necessary, I suggest using require instead, otherwise use if (file_exists($file)) {require $file;} so you can avoid warnings when it doesn't exist and get an error when it can't be read (I assume if it exists, it's intended to be used).