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

怪我咯
Release: 2023-03-13 12:30:02
Original
3885 people have browsed it

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); 
?>
Copy after login

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;  
    }  
}
Copy after login

Comparing the running results, the difference is clear at a glance:

/data/html/test.oa/public
/data/html/test.oa/application/controllers/notice.php
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!