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).

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

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划