Edgar Cooper Edgar Cooper -3 years ago 146
PHP Question

No such file or directory (path error)

Well, the structure of site is simple:


  1. '
    ' folder

    • config.php

    • cesar.php

  2. '
    ' folder

    • index.php

  3. index.php


include_once '../config/cesar.php';


Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory

everything is OK.

If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?

Answer Source

The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.

If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.

./ works since . is the notation for the current directory.

To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.

In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once $_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download