Multiple Approaches to Get Current Script Filename Without Extension in PHP

Nov 16, 2025 · Programming · 12 views · 7.8

Keywords: PHP | Filename Processing | Extension Removal | Path Handling | URL Rewriting

Abstract: This article comprehensively explores various technical solutions for obtaining the current executing script filename and removing its extension in PHP. Through analysis of PHP predefined constants, path information functions, and string manipulation functions, complete code implementations and performance comparisons are provided. The article also integrates URL rewriting techniques to demonstrate extensionless URL access in web environments, covering common scenarios and best practices in real-world development.

Fundamentals of PHP Script Filename Processing

In PHP development, there is often a need to retrieve information about the currently executing script's filename. PHP provides several predefined constants to assist developers in obtaining file path information, with __FILE__ being the most commonly used. This constant returns the complete path and filename of the current file, serving as the foundation for file path processing.

Removing Extensions Using basename Function

For simple extension removal requirements, PHP's basename function offers a direct solution. This function accepts two parameters: the file path and the extension to remove. When only specific extensions (like .php) need to be removed, this approach is both concise and efficient.

$filename = basename(__FILE__, '.php');
echo $filename; // Outputs filename without .php extension

The advantage of this method lies in its code clarity and simplicity, particularly suitable for scenarios where the specific extension is known. However, when dealing with multiple different extensions or unknown extensions, a more generic solution becomes necessary.

Generic Extension Removal Function Implementation

To handle more complex filename scenarios, we can utilize PHP's pathinfo function. This function can parse various components of a file path, including the filename (without extension), directory path, extension, and more.

function chopExtension($filename) {
    return pathinfo($filename, PATHINFO_FILENAME);
}

// Test cases
var_dump(chopExtension('bob.php'));        // Output: string(3) "bob"
var_dump(chopExtension('jquery.js.php'));  // Output: string(9) "jquery.js"
var_dump(chopExtension('bob.i.have.dots.zip')); // Output: string(15) "bob.i.have.dots"

The PATHINFO_FILENAME constant is specifically designed to retrieve the filename portion without the extension, regardless of how many dots the filename contains. It correctly identifies the portion after the last dot as the extension.

Performance Optimization with String Functions

While the pathinfo function is powerful, in scenarios with extremely high performance requirements, using basic string manipulation functions may be more efficient. By combining substr and strrpos functions, identical functionality can be achieved.

function chopExtension($filename) {
    $pos = strrpos($filename, '.');
    if ($pos === false) {
        return $filename; // Case with no extension
    }
    return substr($filename, 0, $pos);
}

// Performance comparison tests show the string function version is approximately 30% faster than pathinfo version

The advantage of this approach lies in avoiding the internal parsing overhead of the pathinfo function, making it particularly suitable for use in loops or high-frequency calling scenarios.

Special Considerations in Web Environments

In web server environments, there are times when the actual requested script filename needs to be obtained, rather than the physical path of the currently executing file. In such cases, the $_SERVER["SCRIPT_FILENAME"] superglobal variable can be used.

$scriptName = basename($_SERVER["SCRIPT_FILENAME"], '.php');
echo $scriptName; // Outputs requested script filename (without extension)

It's important to note that when using symbolic links (symlinks), this method returns the name of the symbolic link rather than the actual file name. Developers need to choose the appropriate solution based on specific requirements.

URL Rewriting and Extensionless Access

In practical web applications, there is often a need to implement extensionless URL access. Apache server's mod_rewrite module provides powerful URL rewriting capabilities to achieve this requirement.

RewriteEngine on
# Check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite extensionless URLs to corresponding .php files
RewriteRule ^([a-z0-9-]+)$ $1.php [L]

This configuration allows users to access page.php file through example.com/page, while maintaining extensionless URL display in the browser address bar. It's important to note that regular expression design should precisely match expected URL patterns to avoid security issues caused by over-matching.

Security Considerations and Best Practices

When handling filenames and URL rewriting, security is a crucial factor that cannot be overlooked. Overly permissive regular expressions (such as using (.*)) may lead to security vulnerabilities, including potential XSS attacks.

Recommended practices include:

Performance Comparison and Selection Recommendations

In actual projects, the choice of method depends on specific requirements:

By understanding the advantages, disadvantages, and applicable scenarios of various methods, developers can choose the most suitable solution based on project requirements, ensuring code that is both efficient and secure.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.