Home >Backend Development >PHP Problem >What are the ways to get the file name in the path in php

What are the ways to get the file name in the path in php

王林
王林Original
2019-12-05 09:55:306091browse

What are the ways to get the file name in the path in php

1. Use the basename function directly

basename() The function returns the file name part of the path.

<?php
$full_name = &#39;c:\wamp\php\php.ini&#39;;
$base = basename($full_name); // $base is "php.ini"
?>

Online video tutorial sharing: php video tutorial

2. Use the pathinfo function

pathinfo() The function takes an array Returns information about the file path.

<?php
$path_parts = pathinfo(&#39;/www/htdocs/inc/lib.inc.php&#39;);
 
echo $path_parts[&#39;dirname&#39;], "\n";
echo $path_parts[&#39;basename&#39;], "\n";
echo $path_parts[&#39;extension&#39;], "\n";
echo $path_parts[&#39;filename&#39;], "\n"; // since PHP 5.2.0
?>

3. Use the explode function

explode() The function uses one string to split another string and returns an array composed of strings.

array explode ( string $delimiter , string $string [, int $limit ] )

Recommended related articles and tutorials: php tutorial

The above is the detailed content of What are the ways to get the file name in the path in php. For more information, please follow other related articles on the PHP Chinese website!

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