119

I have the following structure

otsg
 > class
   > authentication.php
   > database.php
   > user.php
 > include
   > config.inc.php
   > encryption.php
   > include.php
   > session.php
 > index.php
 > registration.php

include.php file has the following

ini_set('display_errors', 1);
error_reporting(E_ALL);

ini_set('include_path',ini_get('include_path').':/Applications/MAMP/htdocs/otsg/:');
require_once 'config.inc.php';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';

and in the index.php page I had included

require_once 'include/include.php';

When I open the page index.php I get the following warning and fatal error. I don't understand what causes this error. When I gave the absolute path it works. But absolute path is not a good idea i believe.

Warning: require_once(../class/database.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/otsg/include/include.php on line 9

Fatal error: require_once() [function.require]: Failed opening required '../class/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/htdocs/otsg/include/:') in /Applications/MAMP/htdocs/otsg/include/include.php on line 9
starball
  • 20,030
  • 7
  • 43
  • 238
user525146
  • 3,918
  • 14
  • 60
  • 103
  • I am usually doing Objective-C and trying to get my hands on php scripting and sad to see I have already wasted 2 hours figuring out how to include files correctly in PHP. Things are so easy with X-code. – rohan-patel Jun 18 '14 at 15:41
  • 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:23

4 Answers4

248

Use

__DIR__

to get the current path of the script and this should fix your problem.

So:

require_once(__DIR__.'/../class/user.php');

This will prevent cases where you can run a PHP script from a different folder and therefore the relatives paths will not work.

Edit: slash problem fixed

DarkSide
  • 3,670
  • 1
  • 26
  • 34
Raisen
  • 4,385
  • 2
  • 25
  • 40
  • 43
    Watch out! `__DIR__` is new in PHP 5.3. If you need this script to run on earlier versions of PHP, you should use `dirname(__FILE__)` instead. – Charles Mar 20 '11 at 22:46
  • 29
    Note that you should manually include the trailing slash in after `__DIR__`. So: `require_once(__DIR__.'/../class/user.php');` Per the [documentation](http://php.net/manual/en/language.constants.predefined.php), the trailing slash is omitted except for the root directory. – Ariel Allon Dec 17 '12 at 17:26
  • 9
    Thanks for the help, but I still don't understand why `__DIR__` is necessary. The PHP docs seem to say that PHP will search the current script's directory by default. Can anyone shed some light? – David Aug 29 '13 at 15:25
  • Thanks! Works just like is supposed to! –  Apr 22 '14 at 15:58
  • 1
    @David I think this is needed when you import a file from 2 places at the same time. For example, you have a file with `require "../a.php"`. If this file is imported from another file (x) in `dir1/dir2/x.php` the file included will be `dir1/a.php`, but if x is in `dir1/dir2/dir3/x.php`, the file included will be `dir1/dir2/a.php`, etc. Or am I wrong? – José Ramón Nov 20 '14 at 19:54
  • A very good answer. Does anyone know if there are any efficiency concerns when using __DIR__ ? – radhoo Feb 25 '15 at 17:50
  • 1
    There is now a troubleshooting checklist for this frequent error here : http://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:24
20

for php version 5.2.17 __DIR__ will not work it will only works with php 5.3

But for older version of php dirname(__FILE__) perfectly

For example write like this

require_once dirname(__FILE__) . '/db_config.php';
Eje
  • 354
  • 4
  • 8
Mobile Tech.
  • 729
  • 1
  • 6
  • 16
1

In my case it doesn't work, even with __DIR__ or getcwd() it keeps picking the wrong path, I solved by defining a costant in every file I need with the absolute base path of the project:

if(!defined('THISBASEPATH')){ define('THISBASEPATH', '/mypath/'); }
require_once THISBASEPATH.'cache/crud.php';
/*every other require_once you need*/

I have MAMP with php 5.4.10 and my folder hierarchy is basilar:

q.php 
w.php 
e.php 
r.php 
cache/a.php 
cache/b.php 
setting/a.php 
setting/b.php

....

linuxatico
  • 1,878
  • 30
  • 43
0

I just came across this same problem, where it was all working fine, up until the point I had an includes within another includes.

require_once '../script/pdocrud.php';  //This worked fine up until I had an includes within another includes, then I got this error:
Fatal error: require_once() [function.require]: Failed opening required '../script/pdocrud.php' (include_path='.:/opt/php52/lib/php')

Solution 1. (undesired hardcoding of my public html folder name, but it works):

require_once $_SERVER["DOCUMENT_ROOT"] . '/orders.simplystyles.com/script/pdocrud.php';

Solution 2. (undesired comment above about DIR only working since php 5.3, but it works):

require_once __DIR__. '/../script/pdocrud.php';

Solution 3. (I can't see any downsides, and it works perfectly in my php 5.3):

require_once dirname(__FILE__). '/../script/pdocrud.php';
hamish
  • 1,141
  • 1
  • 12
  • 21