Coder Perfect

require once does not function with relative paths.


The structure I have is as follows:

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

The include.php file contains the following code:

ini_set('display_errors', 1);

require_once '';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';

I had also included in the index.php page

require_once 'include/include.php';

I get the following warning and fatal error when I open the index.php page. I’m not sure what’s causing this problem. It works when I give the absolute path. However, I believe that taking an absolute road is not a smart idea.

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

Thanks in advance

Asked by user525146

Solution #1



to obtain the script’s current path, and this should resolve your issue.



This prevents situations when a PHP script can be run from a different folder and the related routes do not work.

The slash issue has been resolved.

Answered by Raisen

Solution #2

__DIR__ will not work with PHP 5.2.17; it will only work with PHP 5.3.

However, dirname( FILE ) works well for previous versions of PHP.

For instance, write as follows:

require_once dirname(__FILE__) . '/db_config.php';

Answered by Mobile Tech.

Solution #3

It doesn’t work in my situation; even with __DIR__ or getcwd(), it keeps picking the wrong path; I fixed the problem by defining a costant in every file I require with the project’s absolute base path:

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

I’m using MAMP with PHP 5.4.10, and my folder structure is as follows:



Answered by linuxatico

Solution #4

I recently ran across the identical issue, where everything was working fine until I had an includes within an 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 (inconvenient hardcoding of the name of my public html folder, but it works):

require_once $_SERVER["DOCUMENT_ROOT"] . '/';

Solution 2 (unwanted comment above about DIR not working until PHP 5.3, but it does):

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

Solution 3 (I see no drawbacks, and it works flawlessly in PHP 5.3):

require_once dirname(__FILE__). '/../script/pdocrud.php';

Answered by hamish

Post is based on