开发者

php including file [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I have to include开发者_开发百科 in a php script some files that are located on a 2nd level subdirectory. The problem is that in my xampp it fails to include these files. In netbeans if i click on the path it open files.

require_once("../../config/bootstrap.php");
require_once("../../config/config.php");

What may be the problem? Thanks.


Try

require_once(dirname(__FILE__) . "/../../config/bootstrap.php");
require_once(dirname(__FILE__) . "/../../config/config.php");

Or in PHP 5.3+

require_once(__DIR__ . "/../../config/bootstrap.php");
require_once(__DIR__ . "/../../config/config.php");


you would not normally want to have ..'s in your include files. you may want to bootstrap your webapp with an environment value (in apache, you can use SetEnv for this) and then from you webapp you can $dir = getenv('APPLICATION_PATH'); require_once($dir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'bootstrap.php); Also, you can set APPLICATION_CONFIG_PATH instead, saving up one DIRECTORY_SEPARATOR.

to sum up.. I'd try to avoid ..'s and also hardcoding the '/' (using an environment variable to bootstrap the app and the DIRECTORY_SEPARATOR respectively)

EDIT: forgot to mention: if you're using PHP 5.3, you may get away with using the ../.. with DIR . '../..'.

DIR will resolve to the directory containing the actual running script


this should work perfectly with this directory tree, assuming you'Re accessing public/public/index.php

  • root
    • public
      • public
        • index.php
    • config
      • config.php
      • bootstrap.php


From the docs

In my personal experience, i've found the most consistency from always creating relative references based on the include_path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜