mahen3d
mahen3d

Reputation: 7734

php include /require path specification formats

this is very simple but very confusing most times..

i need to know what it means by when we include files by following patterns (where i have seen in many places may be there is more than this)

What it means ?

"./path" 

"/path"

"../path"

dirname(__FILE__)

basename(dirname(dirname(__FILE__)));

require dirname(__FILE__) . DIRECTORY_SEPARATOR;

pathinfo()

define('BASE_PATH',realpath('.'));

define('BASE_URL', dirname($_SERVER["SCRIPT_NAME"]));

Upvotes: 1

Views: 156

Answers (1)

endoalir
endoalir

Reputation: 381

"./path"

The dot refers to the working directory. This is the directory printed when you type "pwd" on the command line. This may be the directory of the script itself, or the directory of the script that is run first, but not always.

"/path"

Paths that begin with a slash are relative to the topmost directory. This is usually the root directory of the server, but it can also be a mount point, your user directory, or maybe your web root depending on how the server is configured.

"../path"

".." refers to the directory which is one level above the working directory. So if the working directory is /foo/bar then ".." is "/foo" and "../path" refers to "/foo/path"

dirname(__FILE__)

dirname() "Returns parent directory's path." __FILE__ is a magic constant that refers to the full path of the file that uses it. So if you have a script at /foo/bar/baz/file.php, __FILE__ will be "/foo/bar/baz/file.php" and, dirname(__FILE__) will be "/foo/bar/baz"

basename(dirname(dirname(__FILE__)));

basename() "Returns trailing name component of path". Same rules apply for dirname(), so while dirname(__FILE__) in the previous example is "/foo/bar/baz" dirname(dirname(__FILE__)) would be "/foo/bar". The trailing component of this path is "bar", so for this __FILE__ , basename(dirname(dirname(__FILE__))) would be "bar".

require dirname(__FILE__) . DIRECTORY_SEPARATOR;

DIRECTORY_SEPARATOR is a constant I didn't know about, but apparently it is defined in a file system extension. If __FILE__ is "/foo/bar/baz/file.php" then the path comes out to be "/foo/bar/baz/". So this statement seems to be incomplete. require() is like include() except that it gives an error if the file can't be found. The path never refers to a file, so this statement doesn't make much sense.

pathinfo()

pathinfo() parses a path. Pass it a path, and it will give an associative array containing dirname, basename, extension, and filename keys with respective pieces of the parsed path.

define('BASE_PATH',realpath('.'));

realpath() will take a relative path and morph it into an absolute path. As stated earlier, "." refers to the working directory, and if that is "/foo/bar/baz" then realpath(".") will return "/foo/bar/baz". define() will make a constant, so this has the effect of setting the BASE_PATH constant to the working directory of the script. This could be useful if the working directory changes in the course of the script, or if you are including files in subdirectories they can use the constant to refer to the top level of the web application with it.

define('BASE_URL', dirname($_SERVER["SCRIPT_NAME"]));

Same deal with define() and dirname(). The new thing here is $_SERVER["SCRIPT_NAME"]. If the address the script is loaded from is example.com/foo/file.php then this will be "/foo/file.php" and dirname() of that is "/foo". Setting this constant could be useful for pages in the web app to refer to each other.

Upvotes: 2

Related Questions