pathinfo

pathinfo(string $path, int $flags = PATHINFO_ALL): array|string|int

Returns information about a path: directory name, file name, extension, and more.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *path = "/var/www/index.php";
    const char *slash = strrchr(path, '/');
    const char *file = slash ? slash + 1 : path;
    const char *dot = strrchr(file, '.');
    printf("%.*s\n", (int)(file - path - 1), path);
    printf("%s\n", file);
    printf("%s\n", dot ? dot + 1 : "");
    printf("%.*s\n", (int)(dot - file), file);
    return 0;
}
Output
/var/www
index.php
php
index

C++ C++26

C++26
#include <filesystem>
#include <iostream>
int main() {
    std::filesystem::path p("/var/www/index.php");
    std::cout << p.parent_path().string() << "\n";
    std::cout << p.filename().string() << "\n";
    std::cout << p.extension().string().substr(1) << "\n";
    std::cout << p.stem().string() << "\n";
    return 0;
}
Output
/var/www
index.php
php
index

C# v11.0.100

v11.0.100
string p = "/var/www/index.php";
Console.WriteLine(Path.GetDirectoryName(p));
Console.WriteLine(Path.GetFileName(p));
Console.WriteLine(Path.GetExtension(p).TrimStart('.'));
Console.WriteLine(Path.GetFileNameWithoutExtension(p));
Output
/var/www
index.php
php
index

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "path"
)

func main() {
    dir, file := path.Split("/var/www/index.php")
    ext := path.Ext(file)
    fmt.Println(dir)
    fmt.Println(file)
    fmt.Println(ext)
    fmt.Println(file[:len(file)-len(ext)])
}
Output
/var/www/
index.php
.php
index

Java v27.0.1

v27.0.1
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        Path p = Path.of("/var/www/index.php");
        System.out.println(p.getParent());
        System.out.println(p.getFileName());
        String name = p.getFileName().toString();
        int dot = name.lastIndexOf('.');
        System.out.println(name.substring(dot));
        System.out.println(name.substring(0, dot));
    }
}
Output
/var/www
index.php
.php
index

JavaScript v26.13.1

v26.13.1
const path = require("node:path");
const p = path.parse("/var/www/index.php");
console.log(p.dir);
console.log(p.base);
console.log(p.ext);
console.log(p.name);
Output
/var/www
index.php
.php
index

PHP v8.6.0

v8.6.0
$info = pathinfo("/var/www/index.php");
echo $info["dirname"], "\n", $info["basename"], "\n", $info["extension"], "\n", $info["filename"];
Output
/var/www
index.php
php
index

Python v3.15.2

v3.15.2
from pathlib import Path

p = Path("/var/www/index.php")
print(p.parent)
print(p.name)
print(p.suffix)
print(p.stem)
Output
/var/www
index.php
.php
index

Rust v1.99.1

v1.99.1
use std::path::Path;

fn main() {
    let p = Path::new("/var/www/index.php");
    println!("{}", p.parent().unwrap().display());
    println!("{}", p.file_name().unwrap().to_str().unwrap());
    println!("{}", p.extension().unwrap().to_str().unwrap());
    println!("{}", p.file_stem().unwrap().to_str().unwrap());
}
Output
/var/www
index.php
php
index

TypeScript v26.13.1

v26.13.1
import * as path from "node:path";
const p = path.parse("/var/www/index.php");
console.log(p.dir);
console.log(p.base);
console.log(p.ext);
console.log(p.name);
Output
/var/www
index.php
.php
index

Language differences

C: again hand-writes two strrchr steps, using %.*s precision to print the directory and stem.

C++: the example strips the dot to align outputs.

C#: static Path methods decompose the same information; the example strips the dot to align outputs.

Go: the path package decomposes it; the extension keeps the leading dot (.php); path.Split keeps the trailing slash in the directory part (/var/www/).

Java: Path decomposes it; the extension keeps the leading dot.

JavaScript: path.parse decomposes the same information; the extension keeps the leading dot (.php).

PHP: pathinfo returns a dirname/basename/extension/filename array in one call; extension has no leading dot.

Python: Path properties decompose it; the extension keeps the leading dot.

Rust: Path methods decompose it; the example strips the dot to align outputs.

TypeScript: same as JS — path.parse.