Oihana PHP

getBaseFileName.php

Table of Contents

Functions

getBaseFileName()  : string
Returns the base file name without its extension from a given file path.

Functions

getBaseFileName()

Returns the base file name without its extension from a given file path.

getBaseFileName(string $file[, array<string|int, mixed>|null $multiplePartExtensions = null ]) : string

This function extracts the file name from a full path and removes its extension. It supports both single and multi-part extensions (e.g. .tar.gz, .blade.php).

Parameters
$file : string

The full path to the file (e.g. '/path/to/archive.tar.gz').

$multiplePartExtensions : array<string|int, mixed>|null = null

Optional list of multi-part extensions to consider (e.g. ['.tar.gz', '.blade.php']). If null, the method defaults to FileExtension::getMultiplePartExtensions().

Tags
throws
InvalidArgumentException

If the file path is empty or invalid.

example
use function oihana\files\getBaseFileName;

// Basic example with simple extension
echo getBaseFileName('/path/to/image.png'); // 'image'

// With nested path and multi-part extension
echo getBaseFileName('/backups/2025-07-18.tar.gz'); // '2025-07-18'

// File with multiple dots, using default multi-part extensions
echo getBaseFileName('/views/template.blade.php'); // 'template'

// File with unknown multi-dot extension, fallback to last dot
echo getBaseFileName('/logs/system.debug.txt'); // 'system.debug'

// File without extension
echo getBaseFileName('/opt/bin/mybinary'); // 'mybinary'

// Windows-style path (backslashes will be normalized)
echo getBaseFileName('C:\\Users\\me\\file.tar.gz'); // 'file'

// Override default multi-part extensions
echo getBaseFileName('/path/to/file.custom.ext', ['.custom.ext']); // 'file'

// Edge case: dot file (no extension)
echo getBaseFileName('/path/.env'); // '.env'

// Throws exception: empty string
getBaseFileName('');

// Throws exception: path is a directory
getBaseFileName('/path/to/folder/');
see
FileExtension
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The file name without its extension (e.g. 'archive' for 'archive.tar.gz').


        
On this page

Search results