PHP目录/文件层次结构,带“include”和(dirname(__ FILE__)。'SOMETHING')

thx for your time and attention.

I'm working with a php site in which I want to integrate a guestbook I found on the net that works without mySql Database.

My site is structured like this:

host.../myDir/fileA.php (here we have all the basic files of the site)

The Guestbook first file I would integrate is located here:

host.../myDir/gbook/gbook.php 

Under gbook dir there's another dir called template that contains everything needed for graphic (as obvious).

Inside fileA.php I had inserted this include command:

require_once "gbook/gbook.php";

And inside gbook/gbook.php there's this command:

$settings['tpl_path'] = './templates/'.$settings['template'].'/';

Now I've read php guide for (include(dirname(FILE). '...') and I've read also your multiple explanation about this command, so I've tested this:

 $settingsBase['tpl_basepath'] = (dirname(__FILE__) . '/');
 $settings['tpl_path'] =  $settingsBase['tpl_basepath'].'templates/'.$settings['template'].'/';

But is works for integration but not for graphic. So this is what I'm not understanding and what I've understood.

When you "include" a file inside another file, the compiler reads the code as it's part of the same file. That's the problem. When you include something that is located in another /dir the compiler gets the file, but not the whole structure so if there's another "include" inside included file, the compiler will look for it inside the /dir where is located the includer file. Correct?

So exist the dirname() command right? I mean is it correct to use dirname() to tell the compiler that I want to include this file /dir/file.php and everything that will come after and it's included inside that included first file should maintain the same /dir structure?

So if there's this structure:

host.../myDir/fileA.php

and this file contain command:

include gbook/fileB.php

and this file contain command:

include /template/all_the_other_files.php

and the complete structure is:

(1) host...  
   (2) /myDir (where_there's_site)
      (3) /gbook (where_there's_base_gbook_structure)
         (4) /template (where_there's_template)
            (5) /others dir (where_there's_pics_smiles_etc...)

How can I tell to complier "hey when you include gbook/gbook.php" everything is included inside gbook.php will have to be found inside host.../myDir/gbook/...?

I know that probably it's really simple for you, but I'm trying to overcome the problem and I can't understand basic solution and explanation of the problem.

Thx.

First of all I would recommend to use require_once instead include. It raises an error in case the required file is not found, and you don't need to care about multiple includes.

I think you only need to use this in your gbook/fileB.php:

require_once __DIR__ . /template/all_the_other_files.php;

As you see, you can use the DIR magic constant in newer PHP versions instead of dirname(FILE). Using it, you can realize includes to relative paths pretty easy. Hope that helps.