OS: Centos PHP: 7.3
I am following a beginner's course by Kevin Skoglund about PHP. These are a few lines from the code. I understand define()
and dirname()
functions. I just don't understand what is happening here.
define("PRIVATE_PATH", dirname(__FILE__));
define("PROJECT_PATH", dirname(PRIVATE_PATH));
define("PUBLIC_PATH", PROJECT_PATH . '/public');
define("SHARED_PATH", PRIVATE_PATH . '/shared');
this is the output of the 4 constants
/var/www/html/globe_bank/private
/var/www/html/globe_bank
/var/www/html/globe_bank/public
/var/www/html/globe_bank/private/shared
I don't understand why does dirname(__FILE__)
goes only until directory private. Doesn't FILE suppose to be go to all the to root directory.
Same is with the 2nd constant
PROJECT_PATH
. Why do we have to pass the parameter PRIVATE_PATH
?
Here is another section of code.
$public_end = strpos($_SERVER['SCRIPT_NAME'], '/public') + 7;
$doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $public_end);
define("WWW_ROOT", $doc_root);
Not sure what the Doesn't __FILE__ suppose to be go to all the to root directory remark means (root directory is always /
on Unix, you don't need a function to calculate it dynamically). Also, please note that dirname() is just a string function, it doesn't check or care about your actual disk contents.
Having no idea of what you were expecting, the definition of dirname()
is:
Given a string containing the path of a file or directory, this function will return the parent directory's path that is levels up from the current directory.
Since __FILE__ is defined as:
The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
The output is clear:
Beware that your course is possibly terribly outdated because __DIR__
exists as alternative to dirname(__FILE__)
since PHP/5.3.0:
The directory of the file. If used inside an include, the directory of the included file is returned.