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

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。