Home >Backend Development >PHP Tutorial >PHP gets the directory where the current file is located. Usage of getcwd() function

PHP gets the directory where the current file is located. Usage of getcwd() function

怪我咯
怪我咯Original
2017-07-10 15:41:043917browse

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(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);  
include APPPATH.&#39;libraries/api/base.api.php&#39;;  
include APPPATH.&#39;libraries/api/organ.api.php&#39;;  
include APPPATH.&#39;libraries/api/task.api.php&#39;;  
include APPPATH.&#39;libraries/api/pay.api.php&#39;;  
  
class Notice extends MY_Controller{  
    public $coid = 0;  
  
    public function index(){  
        echo getcwd();  
        echo &#39;<br>&#39;;  
        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!

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