How do I get File name from Path in Laravel PHP?

Last updated Jan 13, 2022

Occasionally, you will need to get the current file name, as well as the directory name, where your page is located. Now in PHP, you can easily retrieve this information using  $_SERVER['SCRIPT_NAME'].

Using $_SERVER['SCRIPT_NAME']: $_SERVER stores information such as script locations, paths, and headers created by the web server itself. These entries cannot be provided any other way.

The Syntax

·       In order to get the current filename, we use the following method

$currentPage= $_SERVER['SCRIPT_NAME'];

 

After getting the current filename, we use the following method to display it:

echo $currentPage;

 

Example for get File name from path in laravel php

Let's take a closer look at the following example:

Code snippet

// this line gets the Current Filename.

$currentPage= $_SERVER['SCRIPT_NAME'];

// this line gets the directory name where file have been stored

$currentPage = substr($currentPage, 1);

 

// this line displays the current Filename

echo $currentPage;

  ?>

 

 

Output

After running the above code, your filename will be displayed.

How get file name from path laravel php

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

718 Views