doulu4316 2014-09-02 01:06
浏览 63
已采纳

php的文件的相对vs绝对路径包括[重复]

This question already has an answer here:

So I have this sort of file set up. /cloud/ and /embed/ being two different subdomains.

www
  -cloud
   --config.php
   --files
     ---formSubmit.php
  -embed
    --index.php

in /embed/index.php I have the following code:

include("/www/cloud/files/formSubmit.php");

in /cloud/files/formSubmit.php I have the following code:

include("../config.php");

If I am on cloud.website.com and I go to the formSubmit.php, everything works fine and the config file is included.

However, If I am on embed.website.com and I go to the index.php, I get an error saying that config.php was not found.

Does anyone know what do I need to do to include my formSubmit.php from either location and have my config.php included?

</div>
  • 写回答

1条回答 默认 最新

  • douzhong2954 2014-09-02 01:10
    关注

    In this case, it seems your usage of relative paths is working and absolute paths are not. Whether that means the absolute path of /www/cloud/files/ is incorrect or not, I do not know. In my code, I tend to try to reference files relatively as much as possible like so:

    // In embed/index.php
    include_once dirname(dirname(__FILE__)) . '/cloud/files/formSubmit.php';
    

    What that does is get the directory of the currently executing file and then it's parent directory, which would be www, and then goes back down the path from there to the file I need.

    Subdomains should not make a difference when accessing files server side (as long as the files are hosted on the same server).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?