PHP implementation gets the current file parent directory method Summary

Source: Internet
Author: User
This article summarizes the use of PHP to get the current file directory of the parent directory of 2 ways, and attached to the PHP get path and directory related functions, the need for small partners can refer to the following

method One: first obtain the length of the folder in which the current file is located, and then use substr to intercept the length:

The code is 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 parameter is a negative number, then the "absolute value of this parameter" character is truncated from the back.

method Two: the current file in the same folder as a file (folder is actually a special file, everything is a file!!) ), dirname can be nested directly with dirname:

$parentDirName = DirName (dirname (__file__));

From the above can be seen: the deep understanding of things, can greatly improve the code quality!

Attached: PHP Get path or directory implementation

PHP gets directories and methods through magic variables, through super global variables, through related functions and so on:

<?php/** * php get path or directory implementation *///Magic variable, get the absolute path of the current file echo "__file__: ========>". _ _file__; Echo ' <br/> '; Magic variable, gets the directory of the current script echo "__dir__: ========>". __dir__;echo ' <br/> '; DirName returns the directory portion of the path, dirname (__file__) equals __dir__echo "DirName (__file__): ========>". dirname (__file__); Echo ' < Br/> '; The results of $_server[' php_self ' and $_server[' script_name '] are generally the same, they all get the file name of the current script//Only when PHP is running in CGI mode, But now it's almost impossible to find a CGI run of PHP Echo ' $_server[' Php_self "]: ========> '. $_server[' php_self '];echo ' <br/> '; Echo ' $_server[' Script_name "]: ========> '. $_server[' script_name '];echo ' <br/> '; The absolute path of the current execution script. Remember that running PHP in the CLI mode is not getting the echo ' $_server[' Script_filename "]: ========> '. $_server[' script_filename '];echo ' <br/ > '; The document root directory where the script is currently running. 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> '; 

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.