How to get File Name and Extension from URL in PHP?

In this example, we will give you example how to get file name without extension and get extension from file path in php.

1. Code:

<?php
    function getFileNameAndExtensionFromPath($filePath){
		$fileInfo = pathinfo($filePath);
		$extension = $fileInfo['extension'];
		$filename = $fileInfo['filename'];
        return 'File Name is "'.$filename.'" and extension is '.$extension;
    }
	
    $filePath = "uploads/my_image.jpg";
    echo getFileNameAndExtensionFromPath($filePath);
?>

2. Example:

Input:

filePath = "uploads/my_image.jpg";

Output:

File Name is "my_image" and extension is jpg