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
#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;
}
/var/www index.php php index
C++ 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;
}
/var/www index.php php index
C# 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));
/var/www index.php php index
Go 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)])
}
/var/www/ index.php .php index
Java 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));
}
}
/var/www index.php .php index
JavaScript 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);
/var/www index.php .php index
PHP v8.6.0
$info = pathinfo("/var/www/index.php");
echo $info["dirname"], "\n", $info["basename"], "\n", $info["extension"], "\n", $info["filename"];
/var/www index.php php index
Python 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)
/var/www index.php .php index
Rust 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());
}
/var/www index.php php index
TypeScript 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);
/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.