Get file name with extension from url php

I'm reading up on pathinfo basename and the like. But all I am gathering from those are how to obtain the details I want when using an absolute/relative path. What I am trying to figure out is how can I get the filename.ext from a url string (not necessarily the active URL, maybe a user input url).

Currently I am looking to get the file name and extension of user provided URLs containing images. But may want to extend this further down the road. So in all I would like to figure out how I can get the filename and extension

I thought about trying to use some preg_match logic finding the last / in the url, spliting it, finding the ? from that (if any) and removing the point beyond that and then trying to sort it out after with whats left. but I get stuck in cases where the file has multiple . in the name ie: 2012.01.01-name.jpg

So I am looking for a sane optimal way of doing this without to much margin of error.

asked May 16, 2013 at 8:21

0

$path      = parse_url($url, PHP_URL_PATH);       // get path from url
$extension = pathinfo($path, PATHINFO_EXTENSION); // get ext from path
$filename  = pathinfo($path, PATHINFO_FILENAME);  // get name from path

Also possible with one-liners:

$extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
$filename  = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);

answered Sep 20, 2017 at 19:35

gotjoshgotjosh

8123 gold badges8 silver badges17 bronze badges

use parse_url($url, PHP_URL_PATH) to get URI and use URI in pathinfo/basename.

Moak

12.2k27 gold badges108 silver badges164 bronze badges

answered May 16, 2013 at 8:23

You can use the below code to do what you want...

$fileName = $_SERVER['SCRIPT_FILENAME']; //$_SERVER['SCRIPT_FILENAME'] can be replaced by the variable in which the file name is being stored
$fileName_arr = explode(".",$fileName);

$arrLength = count($fileName_arr);
$lastEle = $arrLength - 1;
echo $fileExt = $fileName_arr[$arrLength - 1]; //Gives the file extension
unset($fileName_arr[$lastEle]);
$fileNameMinusExt = implode(".",$fileName_arr);

$fileNameMinusExt_arr = explode("/",$fileNameMinusExt);
$arrLength = count($fileNameMinusExt_arr);
$lastEle = $arrLength - 1;
echo $fileExt = $fileNameMinusExt_arr[$arrLength - 1]; //Gives the filename

answered May 16, 2013 at 8:45

October 18, 2021 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...