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
I haven’t tried it yet but it should work.
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:
1) In your index.php file, declare a constant corresponding to the absolute path to the public_html directory
Then later use this as the prefix for all includes, and append the path relative to there:
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 fromindex.php
, you would probably do something like this: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.