PHP - How to Get File Name and Extension form URL?
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- PHP Check Word Exist in String or Not Example
- How to Check File is Exists or Not in PHP?
- How to Check If String is URL or Not in PHP?
- How to Set Value as Key in PHP Array?
- How to Convert Array Values to Lowercase in PHP?
- PHP Download File from URL using CURL Request Example
- MySQL Query to Get Current Month Data Example
- How to Add Prefix in Each Key of PHP Array?
- How to Find Day Name from Specific Date in PHP?
- How to Remove Null Values from Array in PHP?