1

I have an includes file with a few class files in:

|includes
|-> database.class.php
|-> user.class.php
|-> functions.php

etc.. Inside my functions.php I include all the class files:

include_once 'database.class.php';

I want to separate some standard files from the class files by changing the structure to:

|includes
|-> functions.php
|-> class
    |-> database.class.php
    |-> user.class.php

Thus when I attempt to use glob:

foreach (glob('/class/*.class.php', GLOB_NOCHECK) as $filename) {
    include_once $filename;
}

I get the following error:

Warning: include_once(/class/*.class.php): failed to open stream: No such file or directory in /home/matt500b/dev2.csgoberry.com/includes/functions.php on line 13

I have to change it to:

glob('../../includes/class/*.class.php', GLOB_NOCHECK)

I want to understand the reason for this when I can just do include_once 'class/database.class.php'; normally.

Why does glob depend on the file that a user is look at rather than including from the functions file as per the manual including? e.g. if I go deeper into a file structure:

|public
    |-> tournament
        |-> dir2
            |-> index.php

I need to use glob('../../../includes/class/*.class.php', GLOB_NOCHECK)


The functions.php file is included in my index.php file using include '../../includes/functions.php';

Total file structure:

|includes
|-> database.class.php
|-> user.class.php
|-> functions.php

|public
|-> tournament
    |-> index.php // Where the ../../includes/functions.php'; is
Matt
  • 1,749
  • 2
  • 12
  • 26

1 Answers1

0

What you should be doing is using $_SERVER['DOCUMENT_ROOT'] (assuming your includes are within the website root) so that whatever page you're on, the PHP is referencing includes with an absolute path so making it fully flexible to any location.

So:

include_once '../../class/database.class.php';

becomes

include_once $_SERVER['DOCUMENT_ROOT']."/includes/class/database.class.php";

and the same method can be applied to all your includes and relative path functions (such as glob) in any page on your website.

WHY?

PHP uses the current directory as the "base" of path folders if the folder is not absolutely specified.

So if your path you give PHP is detected as an absolute reference such as /home/website/public_html/ then the absolute is used, otherwise PHP uses the current working directory . You can manually change the PHP current working directory but really, that's still a relative change so it's more safe and flexible to instead reference file addresses as absolutely as possible.

Included files use the directory location of the file they are included in. The current working directory is (if not altered manually) the directory of the file that includes, rather than of the file being included.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • includes files are outside the public directory due to database connections – Matt Jun 04 '16 at 16:43
  • Then you could manually `DEFINE` a value for the path address such as /home/accountname/includesfolder/` as the `ROOTPATH` Defined value (for example) – Martin Jun 04 '16 at 16:44
  • As you said, defining the path to my folder worked fine. – Matt Jun 04 '16 at 16:57
  • note this approach will fail with aliased locations, such as http://www.example.com/~username. use `__DIR__` for better portability. – Jeff Puckett Jun 04 '16 at 19:46
  • @JeffPuckettII I'm confused. Which part would fail with aliases? – Martin Jun 04 '16 at 20:09
  • There is a troubleshooting checklist for this error, which explains how includes work : https://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Jun 12 '16 at 10:09