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)

不言
Release: 2023-04-04 09:52:01
forward
2993 people have browsed it

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

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

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!

Related labels:
php
source:cnblogs.com
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