Home >Backend Development >PHP Tutorial >PHP gets the directory where the current file is located. Usage of getcwd() function
getchwd() Function Returns the current working directory. This function returns the current working directory if successful. Returns FALSE on failure.
getchwd() function code example
<?php echo getcwd() . "<br/>"; echo dirname(FILE); ?>
PHP can use Magic constantFILE to get the absolute path of the current file, regardless of whether the current file is included by other files Yes, this is very useful in many cases, but sometimes when we perform operations such as reading and writing files, the relative working directory of the current script is the directory of the entry php file. In this case, you can use the Another function getcwd() is used to obtain
and compare the difference between the two:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); include APPPATH.'libraries/api/base.api.php'; include APPPATH.'libraries/api/organ.api.php'; include APPPATH.'libraries/api/task.api.php'; include APPPATH.'libraries/api/pay.api.php'; class Notice extends MY_Controller{ public $coid = 0; public function index(){ echo getcwd(); echo '<br>'; echo FILE; } }
Comparing the running results, the difference is clear at a glance:
/data/html/test.oa/public /data/html/test.oa/application/controllers/notice.php
The above is the detailed content of PHP gets the directory where the current file is located. Usage of getcwd() function. For more information, please follow other related articles on the PHP Chinese website!