Search code examples
phpapachexamppfilenamesrequire-once

Can't include a file named "config.php" in my PHP app (it gets ignored)


I have the following dir structure:

/app
    /controlers
    /models
    /views
    init.php
    config.php
/www
    index.php

/www/index.php code to call the init file:

include_once '../app/init.php';

Then in /app/init.php I have:

require_once 'config.php';
require_once 'routes.php';

For some reason the config.php file is not loading. If I rename it to config2.php and change the include, it is all good. When I switch back to the original name though, it's ignored again. Is there any chance the file name is reserved somehow?

I'm using Apache coming with XAMPP.


Solution

  • When you want to include a file from the same directory use

    require_once './config.php';
    require_once './routes.php';
    

    or

    require_once __DIR__.'/config.php';
    require_once __DIR__./'routes.php';
    

    otherwise a file with same name from your include_path may get included instead...