Get File Name from a Path in PHP



To get the file name from a path, the code is as follows−

Example

 Live Demo

<?php
   $file = pathinfo('/home/cg/root/6985034/main.php');
   echo $file['basename'], "
"; ?>

Output

This will produce the following output−

main.php

Example

Let us now see another example −

 Live Demo

<?php
   $path = "/home/cg/root/6985034/main.php";
   $file = basename($path, ".php");
   echo $file;
?>

Output

This will produce the following output−

main
Updated on: 2019-12-27T07:36:26+05:30

810 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements