Jump to content

File handling / location


Toshiba23

Recommended Posts

Ok, I'm really confused on how level of files work in PHP in folders...

 

Such as... lets say I have a file in a folder one level below the current folder my script is running in... how do I get to the folder below me?

 

require("../folder_below/file.php"); // Would this work?

 

Also, I noticed phpBB uses phpbb_root_path for ever file requirement it does, I'd like to use that, and I tried... but my server just tells me nothing is there you idiot.

 

Basically, what is the difference between './' and '../' and what are easy ways to get to folders throughout my server?

 

Thankies =)  :);)

Link to comment
Share on other sites

Basically using the ".." and "." for including files is not such a great idea u should create a absolute path using the $_SERVER['DOCUMENT_ROOT'] variable otherwise if you try to include a file in a included file which is in some other location the include might not work

 

"." stands for current directory

".." stands for parent directory

Link to comment
Share on other sites

Basically using the ".." and "." for including files is not such a great idea u should create a absolute path using the $_SERVER['DOCUMENT_ROOT'] variable otherwise if you try to include a file in a included file which is in some other location the include might not work

 

"." stands for current directory

".." stands for parent directory

 

Why is it a bad idea?

Link to comment
Share on other sites

Basically using the ".." and "." for including files is not such a great idea u should create a absolute path using the $_SERVER['DOCUMENT_ROOT'] variable otherwise if you try to include a file in a included file which is in some other location the include might not work

 

"." stands for current directory

".." stands for parent directory

 

Why is it a bad idea?

 

Many reasons it should be avoided. One it may not be compatible on another server. Two it is not versatile, you cannot take the script and put it in an upper level directory and expect it to work.

 

Finally if you plan on using htaccess for seo and use a "processing" page to include different files etc, you need the absolute path because the relative path is not what you would expect.

 

It is just good practice to use the absolute path over the relative path.

Link to comment
Share on other sites

How could I find the directory name of a folder I'm in by not including the full path?

 

for example:

dirname(__FILE__);

 

gives full path (document root)

 

I just want...

 

/home/thirdoct/public_html/test/

 

I just want the test directory, not the whole thing...

Link to comment
Share on other sites

nevermind, did some php.net research: dirname($PHP_SELF) returns the directory after document root, so technically...

 

require($_SERVER['DOCUMENT_ROOT'] . dirname($PHP_SELF) .'file.php');

 

would be the same thing as...

 

require('file.php');

Link to comment
Share on other sites

nevermind, did some php.net research: dirname($PHP_SELF) returns the directory after document root, so technically...

 

require($_SERVER['DOCUMENT_ROOT'] . dirname($PHP_SELF) .'file.php');

 

would be the same thing as...

 

require('file.php');

 

As long as you were working with a file in the directory.

 

I would suggest making a constant in a common page

 

define("SERVER_PATH", $_SERVER['DOCUMENT_ROOT']);

 

also remember that $PHP_SELF means register_globals are on which is a security vulnerability. You should use $_SERVER['PHP_SELF'] instead.

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.