Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between php getcwd and dirname(__FILE__)

Detailed explanation of the difference between php getcwd and dirname(__FILE__)

不言
不言Original
2018-06-04 17:25:171340browse

This article mainly introduces the relevant information about the difference between php getcwd and dirname(__FILE__). Friends who need it can refer to it

__FILE__ is a magic constant, used to obtain the complete path of the file. and file name. If used within an included file, returns the name of the included file.

Let’s introduce the difference between getcwd and dirname(__FILE__) through examples.

The code of file/folder/random/foo.php is as follows:

<?php
echo getcwd() . "\n";
echo dirname(__FILE__) . "\n" ;
echo "-------\n";
include &#39;bar/bar.php&#39;;

The code of file/folder/random/bar/bar.php The code is as follows:

<?php
echo getcwd() . "\n";
echo dirname(__FILE__) . "\n";

Run the code/folder/random/foo.php, the result is:

/folder/random
/folder/random
-------
/folder/random
/folder/random/bar

As can be seen from the above example, getcwd() obtains the directory of the currently running script. No matter whether getcwd() is in the included file or the currently executing script file, nothing will happen to the running result. Variety. __FILE__ obtains the file name. If used in an included file, the included file name is returned. If used directly in the currently running script, the file name of the running script is returned.

I hope this article can help everyone, thank you for your support of this site!

The above is the detailed content of Detailed explanation of the difference between php getcwd and dirname(__FILE__). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php file uploadNext article:php file upload