__FILE__ is a magic constant that gets the full path and file name of the file. If used in the included file, the included file name is returned.
Here we introduce the difference between GETCWD and DirName (__file__) by example.
The code for the file/folder/random/foo.php is as follows:
<?php
Echo 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:
<?php
Echo getcwd (). "\ n";
echo dirname (__file__). "\ n";
Run code/folder/random/foo.php, and the result is:
/folder/random
/folder/random
-------
/folder/random
/folder/random/bar
As you can see from the example above, GETCWD () Gets the directory of the currently running script, regardless of whether GETCWD () is in the included file or in the current execution script file, the results of the run are not changed. The __file__ gets the file name and, if used in the included file, returns the included file name and, if used directly in the current run script, returns the filename of the running script.
Hope that through this article can help everyone, thank you for your support to this site!