Home >Backend Development >PHP Tutorial >How to get the file name from php file path (code example)

How to get the file name from php file path (code example)

不言
不言forward
2018-10-13 14:22:462992browse

The content of this article is about how to obtain the file name (code example) from the PHP file path. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Physical interception

$file = '/www/htdocs/inc/lib.inc.php';
$filename = basename($file);
echo $filename, &#39;<br/>&#39;;//  lib.inc.php
$filename = str_replace(strrchr($filename, &#39;.&#39;), &#39;&#39;, $filename);
echo $filename, &#39;<br/>&#39;;//  lib.inc

Use pathinfo($path, $options)

$file = &#39;/www/htdocs/inc/lib.inc.php&#39;;
$path_parts = pathinfo($file);
echo &#39;目录名称&#39; . $path_parts[&#39;dirname&#39;], &#39;<br/>&#39;;  //  /www/htdocs/inc
echo &#39;文件全名&#39; . $path_parts[&#39;basename&#39;], &#39;<br/>&#39;; //  lib.inc.php
echo &#39;文件后缀&#39; . $path_parts[&#39;extension&#39;], &#39;<br/>&#39;;//  php
echo &#39;文件名称&#39; . $path_parts[&#39;filename&#39;], &#39;<br/>&#39;; //  lib.inc         // PHP >= 5.2.0
echo &#39;目录名称&#39; . pathinfo($file, PATHINFO_DIRNAME), &#39;<br/>&#39;;  //  /www/htdocs/inc
echo &#39;文件全名&#39; . pathinfo($file, PATHINFO_BASENAME), &#39;<br/>&#39;; //  lib.inc.php
echo &#39;文件后缀&#39; . pathinfo($file, PATHINFO_EXTENSION), &#39;<br/>&#39;;//  php
echo &#39;文件名称&#39; . pathinfo($file, PATHINFO_FILENAME), &#39;<br/>&#39;; //  lib.inc         // PHP >= 5.2.0

The above is the detailed content of How to get the file name from php file path (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete