Getting File Name Without Extension in PHP: An Alternative Approach
Problem:
You have a PHP function that returns the file extension, but you want to modify it to return the filename without the extension.
Function Code:
function ShowFileExtension($filepath) { // Code to extract filename and extension }
Desired Result:
If the file is "my.zip", the function should return "my" (without the extension).
Solution:
There's a simpler and more efficient way to get the filename without the extension using the pathinfo() function:
$filename = pathinfo($filepath, PATHINFO_FILENAME);
The pathinfo() function breaks down a file path into its various components:
Therefore, to get the filename without the extension, you simply use PATHINFO_FILENAME.
The above is the detailed content of How Can I Get a Filename Without Its Extension in PHP?. For more information, please follow other related articles on the PHP Chinese website!