1

I've been going over those two topics:

and couldn't make my script to work, none of presented methods are working or maybe I'm doing something wrong.

Anyway this is where my problem occurred:

Root/                                   //this is root location for server
APP/                                    //this is root location for script
Root/APP/core/init.php                  //this is where I include classes and functions from
Root/APP/classes/some_class.php         //this is where all classes are
Root/APP/functions/some_function.php    //this is where all functions are

and so obviously I need to include init.php everywhere so I did in every file like this:

require_once 'core/init.php';

it was working until I have decided to create a location for admin files like this:

Root/APP/Admin/some_admin_file.php

and when I included init this way:

require_once '../core/init.php';

script failed to open functions, no such file in APP/Core/ folder so I used DIR method presented in topic above and than even weirder thing happened, error:

no such file in APP/Core/classes/Admin/

What is that? :D I'm lost with this, could someone help a bit ;)

1
  • Check the letters case, are they in caps like this : APP/Core/ or APP/core/ , either one of them is wrong Commented Jul 15, 2014 at 8:58

1 Answer 1

2

Include paths are relative to the current working directory, which can be inspected using getcwd(); this can be a source of many issues when your project becomes bigger.

To make include paths more stable, you should use the __DIR__ and __FILE__ magic constants; for instance, in your particular case:

require_once dirname(__DIR__) . '/core/init.php';

The dirname(__DIR__) expression is effectively the parent directory of the script that's currently being run.

Btw, __DIR__ could also be written as dirname(__FILE__).

Sign up to request clarification or add additional context in comments.

2 Comments

if i do this require_once dirname(__DIR__) . '/core/init.php'; in admin file in admin folder i get this error: Warning: require_once(functions/my_function.php) [function.require-once]: failed to open stream: No such file or directory in /APP/core/init.php so its like trying to open function/function from core folder
@PatrickMevia Yes, well, that just means the problem must be fixed there too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.