Jump to content

How to get to top of file directoy?


Nexus10

Recommended Posts

I have an include on my site.

 

The pages it is requested from (page1) may look like mysite.com/page1 or mysite.com/business/sales (which is in-fact page1 shown by mod_rewrite) etc. How can I write the include which is on that page1 so that it goes to the top of the directory first so we can then go through to the right directory?

 

Also, I tried going include('http://www.mysite.com/thefiletocinlude.php'); although thsi fails to work, anyone know why?

Link to comment
https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/
Share on other sites

This is the code I use to allow me to get by the problem as stated.

 

/config.inc.php

define('BASE_URL','http://' . $_SERVER['HTTP_HOST'] . str_replace('config.inc.php','',str_replace($_SERVER['DOCUMENT_ROOT'],'', str_replace('\\','/',__FILE__))));
define('BASE_REAL_PATH',str_replace('config.inc.php','', str_replace('\\','/',__FILE__)));

 

In subfolders, I do the following.

include('/config.inc.php');
echo BASE_REAL_PATH;

 

I think I know why you wish to do this. Some hosts do not allow the use of "../" for includes, as well as the fact that doing a "../" include can be unreliable at times due to different folders, sub-folders, etc, and if you are using the script in SEO, then the current direction could be faked, and thus, the "../" include would fail  (see phpBB for plenty of examples of this).

 

The above code has allowed me to do a direct include without ever encountering any of the stated problems.  You can name "config.inc.php" whatever you wish, just make sure to replace any instances of it in the code with the new file name. It is also strongly recommended that this is placed in the root / directory to simplify access to it from any sub-folder scripts using the second block of code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.