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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.