Home > Backend Development > PHP Tutorial > Differences and examples of dirname, basename, pathinfo functions in php

Differences and examples of dirname, basename, pathinfo functions in php

怪我咯
Release: 2023-03-07 21:40:01
Original
2508 people have browsed it

To get the path, directory or file name of a file in php, we often use dirname(), basename(), pathinfo() These three functions have been introduced separately in the previous article. This article mainly introduces to you in detail

The differences and usage examples of these three functions.

dirname() function

string dirname ( string $path )
Copy after login

php dirname function gets the directory part of the given file path , the parameter $path is a string of file paths

dirname() function is often used with the magic variable __FILE__, which represents the full path and file name of the currently running file.

dirname(dirname(__FILE__)); What you get is the name of the directory above the file

dirname(__FILE__); What you get is the name of the directory where the file is located

For example:

<?php
echo dirname("c:/testweb/home.php")."<br/>";
echo dirname("/testweb/home.php")."<br/><br/>";

echo __FILE__ ."<br/>";
echo dirname(__FILE__)."<br/>";
echo dirname(dirname(__FILE__));
?>
Copy after login

Code running result:

Differences and examples of dirname, basename, pathinfo functions in php

##basename() function

string basename ( string $path [, string $suffix ] )
Copy after login

php The basename() function gets the file name part of the path, which is the opposite of dirname() (dirname gets the directory part of the path).

The first parameter $path represents a string containing the full path to a file, and the second parameter represents that if the file name ends with suffix, this part will also be removed.

The example is as follows:

<?php
var_dump(basename("/etc/sudoers.d", ".d"));
var_dump(basename("/etc/passwd"));
var_dump(basename("/etc/"));
var_dump(basename("."));
var_dump(basename("/"));
?>
Copy after login

Code running result:

Differences and examples of dirname, basename, pathinfo functions in php

pathinfo() function

php The pathinfo function is used to parse the path and parse the path into an array. The array includes the directory name, complete file name, file extension and file name (excluding the file suffix), and the key names of these four values ​​are dirname, basename, extension and filename respectively. We can use these four key names to obtain the values ​​of the directory name, complete file name, file extension and file name.

Syntax:

mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
Copy after login

Parameters:

path The path to parse.

options If specified, the specified elements will be returned; they include: PATHINFO_DIRNAME, PATHINFO_BASENAME and PATHINFO_EXTENSION or PATHINFO_FILENAME. If options are not specified, the default is to return all units.

Example:

<?
$test = pathinfo("http://localhost/index.php");
print_r($test);
?>
Copy after login

Code running result:

Differences and examples of dirname, basename, pathinfo functions in php

The above is the detailed content of Differences and examples of dirname, basename, pathinfo functions in php. For more information, please follow other related articles on the PHP Chinese website!

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