Backend Development 5 min read

How to Use PHP's pathinfo() Function to Parse File Paths

This article explains the PHP pathinfo() function, covering its syntax, parameters, return values, practical examples, and important usage notes, helping developers efficiently extract directory names, file names, extensions, and filenames from file paths.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP's pathinfo() Function to Parse File Paths

When developing websites or applications, handling file paths—such as obtaining the file name, extension, or directory—is common. PHP provides the very useful pathinfo() function that can conveniently parse a file path and retrieve related information. This article details how to use pathinfo() .

Basic Syntax of the pathinfo() Function

Syntax

<code>pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO ] ) : mixed</code>

Parameter Description

$path : required, the file path to be parsed.

$options : optional, specifies which information to return; by default it returns directory name, base name, extension, and filename (without extension).

Return Value of pathinfo()

The pathinfo() function returns an associative array containing the following elements:

dirname : directory path.

basename : file name with extension.

extension : file extension.

filename : file name without extension.

Example Code

The following examples demonstrate how to use pathinfo() in different scenarios.

Example 1: Get directory path, file name, and extension

<code>$path = '/var/www/html/test.php';
$info = pathinfo($path);

echo 'Directory: ' . $info['dirname'] . "<br>";
echo 'File name: ' . $info['basename'] . "<br>";
echo 'Extension: ' . $info['extension'] . "<br>";</code>

Output

<code>Directory: /var/www/html<br>File name: test.php<br>Extension: php<br></code>

Example 2: Get file name without extension

<code>$path = '/var/www/html/test.php';
$filename = pathinfo($path, PATHINFO_FILENAME);

echo 'File name: ' . $filename . "<br>";</code>

Output

<code>File name: test<br></code>

Example 3: Get directory path and file name (including extension)

<code>$path = '/var/www/html/test.php';
$dirname = pathinfo($path, PATHINFO_DIRNAME);
$basename = pathinfo($path, PATHINFO_BASENAME);

echo 'Directory: ' . $dirname . "<br>";
echo 'File name: ' . $basename . "<br>";</code>

Output

<code>Directory: /var/www/html<br>File name: test.php<br></code>

Important Notes

If the $path argument is empty or not a valid path, pathinfo() returns an empty array.

The $options argument can combine multiple flags using the bitwise OR (|) operator to return several pieces of information at once.

Summary

Through this article we learned how to use PHP's pathinfo() function, which conveniently parses file paths to obtain directory paths, file names, extensions, and more, greatly improving development efficiency when working with files.

PHP Learning Recommendations

Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System (Limited Time Offer)

BackendPHPtutorialfile pathPathInfo
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.