PHP-Fetch path: GETCWD (), __dir__, __file__ difference

Source: Internet
Author: User

getcwd () : Displays the directory in which the file is called

__dir__ : The current content is written in which file to display this file directory

__file__ : The current content is written in which file to display this file directory + file name

Thus

GETCWD () and __dir__ return the absolute path where the file is located, but not the name of the file itself.

__FILE__ is the absolute path where the file is located, but with the name of the file itself.

The following is a reference to Baidu
Create the file test.php in ch06. The directory is: ch06\test.php

The contents are as follows:

<? PHP $a GETCWD (); // the value of variable A is D:\PHP\ZEND6.1\CH6 $b=__file__; // The value of variable B is D:\php\zend6.1\ch6\test.php?>

The folder Admincp folder is created under the CH06 project below. Create the file fff.php under the Admincp folder. The directory is: ch06\admincp\fff.php

The code is as follows

<? PHP include_once ‘.. /test.php '; Echo $a; // the value in variable A is D:\PHP\ZEND6.1\CH6\ADMINCP Echo ' <pre> '; Echo $b; // the value in variable B is D:\php\zend6.1\ch6\test.php?>

This shows that the test.php file is included in another file. The path variable A of test.php obtained with GETCWD () in the test.php file changes after it has been contained. The directory structure is added to the ffff.php directory ADMINCP, so • __file__ better when locating files

Example

<?PHPDefine(' Path_root ', ($PATH _root=dirname(__file__))?$PATH _root:‘..‘);$PATH _ADMINCP=path_root. ' \ADMINCP ';$PATH _picture=path_root. ' \pciture ';$PATH _admincp_include=$PATH _ADMINCP.‘ \include ';? ><?PHPinclude_once dirname(__file__).‘ /.. /.. /path.php ';include_oncePath_root. " /con_ini.php ";?>

PHP-Fetch path: GETCWD (), __dir__, __file__ difference

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.