PHP Gets the parent directory method of the current file summary _php tips

Source: Internet
Author: User
Tags parent directory

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> '; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.