This article mainly introduces the PHP getcwd and dirname (__file__) different details of the relevant information, the need for friends can refer to the following
__FILE__ is a magic constant used to get the full path and file name of a file. If used in the included file, returns the file name that is included.
Below we introduce the difference between GETCWD and DirName (__file__) by an example.
The code for the file/folder/random/foo.php is as follows:
<?phpecho GETCWD (). "\ n"; Echo dirname (__file__). "\ n"; echo "-------\ n"; include ' bar/bar.php ';
The code for the file/folder/random/bar/bar.php is as follows:
<?phpecho GETCWD (). "\ n"; Echo dirname (__file__). "\ n";
Run the code/folder/random/foo.php and the result is:
/folder/random/folder/random-------/folder/random/folder/random/bar
As can be seen from the above example, GETCWD () Gets the directory that is currently running the script, regardless of whether GETCWD () is inside the contained file or in the currently executing script file, the result of the run will not change. And __file__ gets the file name, if used in the included file, returns the included file name, and if used directly in the current run script, returns the file name of the running script.
Hope that through this article can help everyone, thank you for the support of this site!