skip to Main Content

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

Answers


  1. I haven’t tried it yet but it should work.

    $public_htmlUrl = $_SERVER['DOCUMENT_ROOT'];
    $outsideUrl = "../".$public_htmlUrl;
    $file = outsideUrl."subdomain/conn.php";
    include($file);
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search