Content · Programming · PHP

PHP pathinfo() Function: Usage and Examples

Extracts directory, basename, extension, or filename information from a path.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

pathinfo() Function is one of the PHP operations used in filesystem and stream workflows. Extracts directory, basename, extension, or filename information from a path. The examples below show what it does and how to handle its result.

What Is pathinfo() Function?


Extracts directory, basename, extension, or filename information from a path.

Its syntax is:

pathinfo(string $path, int $flags = PATHINFO_ALL)

The parameters are:

  • string $path — A local path to open or save, controlled by the application.
  • int $flags = PATHINFO_ALL — Option constants that alter how the operation behaves.

Return value: The declared return type is array|string. pathinfo() operates on strings; it does not verify whether the file exists.

Important: pathinfo() operates on strings; it does not verify whether the file exists.

Example 1: Basic Usage


The basic example focuses on the behavior of pathinfo() Function: extracts directory, basename, extension, or filename information from a path. Inputs are defined locally or shown as application-controlled paths.

<?php

$details = pathinfo("/reports/summary.csv");
echo $details["filename"] . " / " . $details["extension"];

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for pathinfo() Function. Notice the different checks, arguments, or output handling.

<?php

$extension = pathinfo("image.backup.png", PATHINFO_EXTENSION);
$filename = pathinfo("image.backup.png", PATHINFO_FILENAME);
echo $filename . " / " . $extension;

This variation matters because pathinfo() operates on strings; it does not verify whether the file exists.

Practical Example: Using the Operation in an Application


The following example demonstrates how pathinfo() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$path = "/var/app/backups/report.tar.gz";
echo pathinfo($path, PATHINFO_BASENAME) . "\n";
echo pathinfo($path, PATHINFO_EXTENSION);

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Extracts directory, basename, extension, or filename information from a path.
  • Limitation: pathinfo() operates on strings; it does not verify whether the file exists.

Comparison With Related Functions


Function or MethodDifference
pathinfo() FunctionExtracts directory, basename, extension, or filename information from a path.
basename()Returns the trailing path component but does not provide all path components or extension fields.

Use pathinfo() Function for the specific behavior described above. The related operation, basename(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming this operation has the same behavior on every file or environment.pathinfo() operates on strings; it does not verify whether the file exists. Check prerequisites and return values before using the result.
Treating basename() as an interchangeable replacement for pathinfo() Function.Returns the trailing path component but does not provide all path components or extension fields.

Key Takeaways


  • Extracts directory, basename, extension, or filename information from a path.
  • pathinfo() operates on strings; it does not verify whether the file exists.
  • Related operation: basename() — Returns the trailing path component but does not provide all path components or extension fields.

pathinfo() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.