7

I have an include statement in a PHP script running on xampp on Windows. If I use a relative path :

include '../config/eventInfoConfig.php';  

I get the error message:

Warning: include(../config/eventInfoConfig.php) [function.include]: failed to open stream: No such file or directory

But if I use the absolute path I have no error:

include 'c:/xampp/htdocs/xampp/appTrials/myApp/config/eventInfoConfig.php'; 

How can I use a relative path in my include without causing an error?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user840930
  • 5,214
  • 21
  • 65
  • 94
  • What does `var_dump(get_include_path())` output? – greg0ire Dec 15 '11 at 19:21
  • string(42) ".;C:\xampp\htdocs\xampp;C:\xampp\php\PEAR;" But I think you have answered my question. I need to include a path to my apache htdocs folder in the include_path parameter in my php.ini file. That works! Thanks very much for your help!! – user840930 Dec 16 '11 at 10:19
  • I just ran into the exact same error for the exact same reason. Why the hell does it not try to resolve the path relative to the script's directory before resorting to include_path?! – Steven Liekens Oct 26 '13 at 22:36
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:09

3 Answers3

4

I would make it dynamic so that you can also include this file in other files and still have it work properly.

include_once(dirname(__FILE__).'/../config/eventInfoConfig.php');
Soshmo
  • 318
  • 2
  • 7
3

Fixing the include_path() to include the right path does the trick.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
1

You can turn the relative path into an absolute one. include is tricky. I always just keep a global constant, BASE_PATH, that contains the absolute path, and append the file I need to include.

Ry-
  • 218,210
  • 55
  • 464
  • 476