PHP - How to Get File Name and Extension form URL?

By Hardik Savani May 14, 2024 Category : PHP

Now, let's see tutorial of php get file extension from filename. This article goes in detailed on how to get file extension from file name in php. let’s discuss about php get file extension from url string. This article will give you simple example of php get file name and extension from url.

In this example, i will give you two examples how to get file name without extension and get extension from file path. so let's see both example with output.

Example 1:

index.php

<?php

$filePath = "uploads/my_image.jpg";

$extension = pathinfo($filePath, PATHINFO_EXTENSION);

$filename = pathinfo($filePath, PATHINFO_FILENAME);

print_r('File Name is "'.$filename.'" and extension is '.$extension);

?>

Output:

File Name is "my_image" and extension is jpg

Example 2:

index.php

<?php

$filePath = "uploads/my_image.jpg";

$fileInfo = pathinfo($filePath);

$extension = $fileInfo['extension'];

$filename = $fileInfo['filename'];

print_r('File Name is "'.$filename.'" and extension is '.$extension);

print_r($fileInfo);

?>

Output:

File Name is "my_image" and extension is jpg

Array

(

[dirname] => uploads

[basename] => my_image.jpg

[extension] => jpg

[filename] => my_image

)

i hope it can help you...

Tags :
Shares