Home > Backend Development > PHP Tutorial > How Can I Get a Filename Without Its Extension in PHP?

How Can I Get a Filename Without Its Extension in PHP?

Mary-Kate Olsen
Release: 2024-12-07 02:00:11
Original
920 people have browsed it

How Can I Get a Filename Without Its Extension in PHP?

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
}
Copy after login

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);
Copy after login

The pathinfo() function breaks down a file path into its various components:

  • PATHINFO_DIRNAME - the directory part
  • PATHINFO_BASENAME - the basename (filename with extension)
  • PATHINFO_EXTENSION - the file extension
  • PATHINFO_FILENAME - the filename without extension (available in PHP 5.2.0 and higher)

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template