Search code examples
phpfileincluderequire-once

require_once giving warning and fatal error


public_html > config.php 

public_html > A > test_file.php

in test_file.php code

require_once("../config.php");

works fine. shows dynamically-related files fine (config.php) in dreamweaver

public_html > includes > classes > allclasses.php

in allclasses.php code

require_once("../../config.php");

shows dynamically-related files fine (config.php) in dreamweaver

when I include allclasses.php in test_file.php

require_once("../config.php");
require_once("../includes/classes/allclasses.php");

shows dynamically-related files fine (config.php, allclasses.php) in dreamweaver

test_file.php stop working and on browser shows

Warning: require_once(../../config.php) [function.require-once]: 
failed to open stream: No such file or directory in .....\public_html\includes\classes\allclasses.php on line 11

using xampp as development server.


Solution

  • When doing includes inside includes it´s better to use absolute paths. So you can use the __DIR__ or __FILE__ constant to do it, just like this in test_file.php for example:

    require_once(realpath(dirname(__FILE__) . '/../config.php'));
    

    or just

    require_once(realpath(__DIR__ . '/../config.php'));