Home >Backend Development >PHP Tutorial >How to get the file name from php file path (code example)
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, '<br/>';// lib.inc.php $filename = str_replace(strrchr($filename, '.'), '', $filename); echo $filename, '<br/>';// lib.inc
Use pathinfo($path, $options)
$file = '/www/htdocs/inc/lib.inc.php'; $path_parts = pathinfo($file); echo '目录名称' . $path_parts['dirname'], '<br/>'; // /www/htdocs/inc echo '文件全名' . $path_parts['basename'], '<br/>'; // lib.inc.php echo '文件后缀' . $path_parts['extension'], '<br/>';// php echo '文件名称' . $path_parts['filename'], '<br/>'; // lib.inc // PHP >= 5.2.0 echo '目录名称' . pathinfo($file, PATHINFO_DIRNAME), '<br/>'; // /www/htdocs/inc echo '文件全名' . pathinfo($file, PATHINFO_BASENAME), '<br/>'; // lib.inc.php echo '文件后缀' . pathinfo($file, PATHINFO_EXTENSION), '<br/>';// php echo '文件名称' . pathinfo($file, PATHINFO_FILENAME), '<br/>'; // 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!