dougang1965 2018-09-05 13:37
浏览 97
已采纳

如何使用php调用域目录中的public_html外的文件

I have a domain that has a subdomain. I want to call a file in the subdomain folder but apparently my subdomain folder is outside the public_html directory. Here is what i tried but nothing is happening.The subdomain folder is subdomain.

<?php
include("subdomain/conn.php") // location of the file in the subdomain
?>
  • 写回答

2条回答 默认 最新

  • doutu9810 2018-09-05 14:12
    关注

    It is usually most helpful to include with an absolute path instead of a relative path. There are two typical ways to approach this. This example will work with the following directory structure, as you did not provide yours:

     +var
     |+www
      |+public_html
       -index.php
       |+subfolder
        |somescript.php
      |+subdomain
       |conn.php
    

    1) In your index.php file, declare a constant corresponding to the absolute path to the public_html directory

    define('APPLICATION_BASE', __DIR__ . DIRECTORY_SEPARATOR);
    

    Then later use this as the prefix for all includes, and append the path relative to there:

    include APPLICATION_BASE . '../subdomain/conn.php';
    

    This option works well for most general purposes, allowing you to quickly include most any file relative to the front-controller/index file

    2) Alternately, if you are not using a uniform index file and do not have a consistent point of reference reliable enough to always be included, you would apply a similar approach and include based on an absolute path from the file including it. This usually indicates you are lacking good application structure, but for a quick and dirty app or legacy code that was not structured with a definitive single point of access, this is often necessary. In the case where you had to include a file from somescript.php, and did not reliably arrive there from index.php, you would probably do something like this:

    include __DIR__ . '/../../subdomain/conn.php';
    

    It should be noted that this works in a pinch, but it is far from optimal, and you should try to massage your application toward the first approach if at all possible. Using a consistent point of entry will save you a lot of guesswork later when debugging, as you will know all files are included based on relevance to a single origin directory, and it will keep error messages and logging a lot cleaner and more readable over time, which in turn will save you a lot of time debugging.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上