doucan8049 2015-07-31 10:57 采纳率: 100%
浏览 53

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.

  • 写回答

1条回答 默认 最新

  • dongzhou1901 2015-07-31 11:27
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?