Home  >  Article  >  Backend Development  >  How to get the current directory and relative directory in PHP_PHP tutorial

How to get the current directory and relative directory in PHP_PHP tutorial

ringa_lee
ringa_leeOriginal
2018-05-18 14:03:0521649browse

How to get the current directory and relative directory in PHP

php There are many ways to get the directory. Here is a record of using php to get the current directory and relative directory. method to avoid confusion again.

<?php
//获取当前文件所在目录,如果 A.php include B.php 则无论写在哪个文件里,都是表示 A.php 文件所在的目录
echo realpath(&#39;.&#39;),&#39;
&#39;;
echo getcwd(),&#39;
&#39;;

// 获取当前文件的上级目录,如果 A.php include B.php 则无论写在哪个文件里,都是表示 A.php 文件所在目录的上级目录
echo realpath(&#39;..&#39;),&#39;
&#39;;

// 获取网站根目录,所有文件里面获取的都是当前项目所在的目录
echo $_SERVER[&#39;DOCUMENT_ROOT&#39;],&#39;
&#39;;

// 获取目录信息
$path_parts = pathinfo(__FILE__);
echo &#39;dirname: &#39;,$path_parts[&#39;dirname&#39;],&#39;
&#39;;//表示代码所在文件的目录,如果 A.php include B.php 并且此代码段写在 B.php ,那么获取的是 B.php 文件所在的目录

echo &#39;basename: &#39;,$path_parts[&#39;basename&#39;],&#39;
&#39;;//同上,获取的是代码所在的文件的文件名称,比如:inc.php

echo $path_parts[&#39;extension&#39;],&#39;
&#39;;//同上,获取的是代码所在的文件的后缀名,比如:php

echo dirname(__FILE__),&#39;
&#39;;//效果同 $path_parts[&#39;dirname&#39;]
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