Jump to content

Accessing Folders Outside My Web-Root


Recommended Posts

Hi,

 

When I want to access a folder outside my web root in a PHP file (e.g. because I want to include a file but I don't want internet users to have access to the raw file) I use something like the following:

include $_SERVER["DOCUMENT_ROOT"] . "/../phpinclude/template3.php";

I.e. find the web root, go "up" one directory (on TCH servers this is my account home directory) then select the directory phpinclude (which because it's not under the web root is not accessible to anyone from the web) and include something.

 

This is the most general way I've beed able to find that does this job. I want this to work on other (non-TCH) servers too, including Windows/IIS servers, if at all possible.

 

This normally works fine, but I've just set up a new TCH account and it doesn't work. I think the reason is that when I am accessing my account using http://myhost.tchmachines.com/~username/ instead of ***** (because the account is new and has not yet been fully set up), $_SERVER["DOCUMENT_ROOT"] is equal to /usr/local/apache/htdocs/, not /home/username/public_html/.

 

Is there a better way of finding my home directory's physical path on the server (not my web root, the one above that), which works in this scenario?

 

Thanks - Rowan

Link to post
Share on other sites

It is not possible to edit the document root via an override on .htaccess or php.ini, it will take the default from httpd.conf. The problem is due to the usage of server url instead of the domain name which cannot be changed as it is a server wide setting.

Link to post
Share on other sites

It is not possible to edit the document root via an override on .htaccess or php.ini, it will take the default from httpd.conf. The problem is due to the usage of server url instead of the domain name which cannot be changed as it is a server wide setting.

Thanks for your reply.

 

I'm not 100% sure what you mean by "document root". This seems to be an ambibuous phrase. It would make sense if it meant my home directory, i.e. /home/username/, but in the PHP array element $_SERVER["DOCUMENT_ROOT"] it seems to mean what I call the "web root", i.e. the folder accessed by putting http://myhost.tchmachines.com/~username/ (or my domain name) into a browser.

 

If you mean my home directory, I don't want to edit it. What I actually want is for a PHP script to include (i.e. read, not edit, write or delete) a file in a subdirectory (but not public_html) of my home directory.

 

I'm not quite sure why you have referred to .htaccess or php.ini. What do these have to do with my question?

 

All I need is for my PHP script to be able to find out the absolute path of my home directory in a way that's cross platform and works when I'm using my domain name and when I'm using the server URL to access the site.

 

The best I've come up with so far is:

 

>$abspath = $_SERVER["SCRIPT_FILENAME"];
$requesturi = $_SERVER["SCRIPT_NAME"];
while (basename($abspath) == basename($requesturi))
{
$abspath = dirname($abspath);
$requesturi = dirname($requesturi);
}
include $abspath . "/../phpinclude/template3.php";

 

This seems to work on a normal TCH account accessed via the domain name, on a TCH account accessed via the server URL and tilde username, and on a Windows/IIS server.

 

Can anyone improve on this, or tell me of some situation where it won't work?

 

Thanks - Rowan

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...