The Wayback Machine - https://web.archive.org/web/20240713040508/https://www.geeksforgeeks.org/how-to-get-file-name-from-a-path-in-php/
Open In App

How to get file name from a path in PHP ?

Last Updated : 03 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to get the file name from the path in PHP, along with understanding its implementation through the examples. We have given the full path & we need to find the file name from the file path. For this, we will following below 2 methods:

  • Using the basename() function
  • Using the pathinfo( ) Function

Input : path = /testweb/var/www/mywebsite/htdocs/home.php
Output : home.php

Input : path = /testweb/var/www/mywebsite/htdocs/abc.txt
Output : abc.txt

We will understand both functions with the help of examples.

Method 1: Using basename() function:

The basename() function is an inbuilt function that returns the base name of a file if the path of the file is provided as a parameter to the basename() function. 

Syntax:

$filename = basename(path, suffix);

The path is a required field that specifies the path which is to be checked. The suffix is an optional field that specifies a file extension. If the filename has this file extension, the file extension will not show.

Example: This example describes the use of the basename() function that returns the base name of the file.

PHP




<?php
  $path = "/testweb/var/www/mywebsite/htdocs/home.php";
  
  $file1 = basename($path);
  $file2 = basename($path, ".php");
  
  // Show filename with file extension
  echo $file1 . "\n";
  
  // Show filename without file extension
  echo $file2;
?>


Output:

home.php
home

Method 2: Using pathinfo() function:

The pathinfo() is an inbuilt function that is used to return information about a path using an associative array or a string ie., It will create an array with the parts of the path we want to use.

Syntax:

$filename = pathinfo(path);

Example: This example explains the pathinfo() function that will return information about a path. Here, we will use $filename[‘basename’], when we want to access the file name.

PHP




<?php
  // Path of the file stored under pathinfo
  $myFile = pathinfo('/usr/admin/config/test.php');
  
  // Show the file name
  echo $myFile['basename'], "\n";
?>


Output:

test.php


Previous Article
Next Article

Similar Reads

How to get file input by selected file name without path using jQuery ?
To get the selected file name without the path using jQuery, use the HTML &lt;input type="file"&gt; element. Upon file selection, the jQuery change() method captures the event, and the file name is retrieved using the name property of the event target's files. Syntax:$(selector).change(function(e){});Example 1: The below example will explain how to
2 min read
How to get the file name from full path using JavaScript ?
Given a file name that contains the file path also, the task is to get the file name from the full path. There are a few methods to solve this problem which are listed below: JavaScript replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: stri
2 min read
JavaScript | WebAPI | File | File.name Property
The File.name property is an inbuilt function of File WebAPI which gives the name of the file represented by a file object. The path of file is excluded from this property for security reasons. Syntax: var name = file.name; Return Value: It returns a string, containing the name of the file without path. Example: &lt;!DOCTYPE html&gt; &lt;html&gt;
1 min read
How to get the file name from page URL using JavaScript ?
JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approaches to getting the file name from the page URL us
3 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
How to access actual name of uploaded file in PHP ?
In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES["file"]["name"]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded.The file refers to the name which is defined in the "index.html" form in the input of the file.The name refers to the actual name of the file. In th
2 min read
How to get current function name in PHP?
The function name can be easily obtained by the __FUNCTION__ or the __METHOD__ magic constant. Method 1 (Prints function name): __FUNCTION__ is used to resolve function name or method name (function in class). Example: &lt;?php class Test { public function bar() { var_dump(__FUNCTION__); } } function foo() { var_dump(__FUNCTION__); } // Must output
1 min read
How to get a variable name as a string in PHP?
Use variable name as a string to get the variable name. There are many ways to solve this problem some of them are discussed below: Method 1: Using $GLOBALS: It is used to reference all variables available in the global scope. It is an associative array containing the reference of all variables currently defined in the global scope. Example: This e
2 min read
How to Name and Retrieve a Git Stash by Name?
Git stash is a powerful feature that allows developers to temporarily save changes in their working directory and staging area that they don't want to commit immediately. By naming stashes, you can organize and retrieve them more easily. Here's a detailed guide on how to name and retrieve a Git stash by name. Table of Content Stashing Changes with
3 min read
How to Name a Stash and Retrieve a Stash by Name in Git?
Git is an important tool for developers, enabling efficient version control and collaboration on projects. One powerful feature of Git is the ability to stash changes, temporarily storing modifications without committing them. This is particularly useful when you need to switch contexts or branches but aren’t ready to commit to your current work. T
3 min read
three90RightbarBannerImg