Computer >> Computer tutorials >  >> Programming >> PHP

How to get file name from a path in PHP?


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

Example

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

Output

This will produce the following output−

main.php

Example

Let us now see another example −

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

Output

This will produce the following output−

main