First, get the length of the folder that contains the current file, and then use substr to intercept the length:
Copy Code code as follows:
$dirName = Str_replace ("\", "/", DirName (__file__));
$dirNameLength = strlen ($dirName);
$currentDirNameLength = $dirNameLength-strrpos ($dirName, "/"); Get the length of the folder where the current file is located!
$parentDirName = substr ($dirName, 0,-$currentDirNameLength); If the 3rd argument is a negative number, then the "absolute value of the parameter" character is intercepted from the back.
method Two: the current file is the folder as a file (folder is actually a special file, everything is a file!!) ), the dirname can be nested directly with dirname:
$parentDirName = DirName (dirname (__file__));
From the above can be seen: the profound understanding of things, can greatly improve the quality of the code!
Attached: PHP Get path or directory implementation
PHP gets directories and methods through magic variables, through super global variables, through correlation functions, and so on:
<?php/** * php Gets the path or directory implementation *//Magic variable, gets the absolute path of the current file echo "__file__: ========>". __file__;
Echo ' <br/> ';
The magic variable gets the current script's directory echo "__dir__: ========>". __dir__;
Echo ' <br/> ';
DirName returns the directory portion of the path, DirName (__file__) is equivalent to __dir__ echo "DirName (__file__): ========>". dirname (__file__);
Echo ' <br/> ';
The results of $_server[' php_self ' and $_server[' script_name ' are generally the same, they are the filenames for the current script//Only when PHP is running in CGI, but there's almost no way to run PHP in CGI.
Echo ' $_server["Php_self"]: ========> '. $_server[' php_self '];
Echo ' <br/> ';
Echo ' $_server["Script_name"]: ========> '. $_server[' Script_name '];
Echo ' <br/> '; The absolute path of the currently executing script.
Remember, running PHP in CLI mode is not getting the echo ' $_server[' Script_filename ']: ========> '. $_server[' Script_filename '];
Echo ' <br/> '; The document root directory where the currently running script resides.
Defined in the server configuration file.
Echo ' $_server["Document_root"]: ========> '. $_server[' Document_root '];
Echo ' <br> ';
GETCWD () returns the current working directory echo "GETCWD (): ========>". GETCWD ();
Echo ' <br> ';