Home > Backend Development > PHP Tutorial > How Can I Extract a Filename from a File Path Using PHP's `basename()` Function?

How Can I Extract a Filename from a File Path Using PHP's `basename()` Function?

Patricia Arquette
Release: 2024-12-30 22:35:11
Original
336 people have browsed it

How Can I Extract a Filename from a File Path Using PHP's `basename()` Function?

Getting File Name from Path using PHP

You may encounter scenarios where you need to extract the file name from a given path. PHP offers a convenient function for this task: basename().

Consider the following example where you want to retrieve the file name "Output.map" from the full path "F:Program FilesSSH Communications SecuritySSH Secure ShellOutput.map".

Using Basename

To accomplish this, you can utilize basename() as follows:

$path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$file = basename($path);
Copy after login

In this example, basename() will extract "Output.map" and assign it to the $file variable.

Additional Options

Basename() provides options to further customize the extraction process:

  • $suffix: You can specify a suffix to remove before extracting the file name. For instance, basename($path, ".map"); will return "Output" by removing ".map" from the end.
  • $flags: PHP allows for flags to alter basename()'s behavior. Refer to the PHP manual for more details.

The above is the detailed content of How Can I Extract a Filename from a File Path Using PHP's `basename()` Function?. 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