Use PHP to Get File Path/Extension from URL string [duplicate] - php

This question already has answers here:
How to get a file's extension in PHP?
(31 answers)
Closed 2 years ago.
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.

$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);

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

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

Related

PHP: How to explode string

I have a variable that stores the location of a temp file:
$file = 'C:\xampp\htdocs\temp\filename.tmp';
How can I explode all this to get filename (without the path and extension)?
Thanks.
Is not the best code but if you confident that this path will be similar and just file name will be different you can use this code:
$str = 'C:\xampp\htdocs\temp\filename.tmp';
$arrayExplode = explode("\\", $str);
$file = $arrayExplode[count($arrayExplode)-1];
$filename = explode('.', $file);
$filename = $filename[0];
echo $filename;
Advice: Watch out on the path contain "n" like the first letter after the backslash. It could destroy your array.
You should use the basename function, it's meant specifically for that.

Substring between two different characters? [duplicate]

This question already has answers here:
PHP, get file name without file extension
(18 answers)
Closed 4 years ago.
I'm looking for a way to get a substring in php. Given string is:
"abc/def/ghi/name.extension"
I only want to have 'name', and the number of forward slashes is not known and not the same number either. Tried it with substring, but I get stuck in the elimination of the /
Anyone an idea how to solve this?
Help is greatly appreciated!
You can use pathinfo:
$str = "abc/def/ghi/name.extension";
$name = \pathinfo( $str, \PATHINFO_FILENAME );
Try this. First split the string into an array with explode () and then split the last element by the dot.
$a = 'abc/def/ghi/name.extension';
$b = explode ('/', $a);
$b = $b[count ($b) - 1];
$c = explode ('.', $b)[0];
You can try this for filename.
$filename = pathinfo("abc/def/ghi/name.extension", PATHINFO_FILENAME);
echo $filename;
and this for file extension.
$ext = pathinfo("abc/def/ghi/name.extension", PATHINFO_EXTENSION);
echo $ext;

wordpress extract media url

$path = parse_url($post->guid, PHP_URL_PATH);
echo "<pre>";
print_r($path);
echo "<br>";
here i get
/wp-content/uploads/2014/01/kl-2-256.png
/wp-content/uploads/2014/04/bg-eBook.pdf
here i want to remove /wp-conent/uploads from these paths and extract only year month and image name
i tried with
$segments = explode('/', rtrim($path, '/'));
but not working properly every time
is there any proper and best solution?
Would something like this work for you?
$path = parse_url($post->guid, PHP_URL_PATH);
$path = str_replace("wp-content/uploads/", "", $path);
echo $path;
Use the list() construct to map the three data you need. The code is exploding the path by / and then looks from behind and passes those values to your mapped variables of list.
$path = '/wp-content/uploads/2014/01/kl-2-256.png';
list($year,$month,$image)=array_slice(explode('/',$path),-3,3);
You can then print $year,$month and $image separately.

file_put_contents and image url whitespaces

I have a problem with getting image from url. I'm using file_put_contents and I found that problem is white spaces in image url because images without any whitespace working.
The URL I'm getting image looks that:
/support/member_profile/16-New%20Image%20(With%20Logo)%20(Medium).jpg
I tried with urlencode() but it's still not working. If I echo encoded url I get:
%2Fsupport%2Fmember_profile%2F16-New+Image+%28With+Logo%29+%28Medium%29.jpg
How can I solve that problem? Thanks in advance
EDIT:
I figured out that I would need to replace ONLY whitespaces with %20. When using urlencode it encode entire URL so that's why it's not working.
Any tip how to do it? Thanks
Use urldecode to convert %20 into real spaces.
Then you can call file_put_contents.
You should only URL-encode the filename, not the entire path including the slashes:
$path = '/support/member_profile/16-New Image (With Logo) (Medium).jpg';
$p = pathinfo($path, PATHINFO_DIRNAME);
$f = pathinfo($path, PATHINFO_FILENAME);
$e = pathinfo($path, PATHINFO_EXTENSION);
echo sprintf('%s/%s.%s', $p, urlencode($f), urlencode($e));
And, actually, you need to urlencode each path-part as well.
$path = '/support/member profile/16-New Image (With Logo) (Medium).jpg';
$p = explode('/', $path);
foreach ($p as $pp)
$pathparts[] = urlencode($pp);
echo implode('/', $pathparts);

get filename with pure extension

I'm coding a script to get image from a site. All is good, but then I notice there are some sites which have images in format like this:
http://site-name/images/dude-i-m-batman.jpg?1414151413
http://site-name/images/dude-i-m-batman.jpg?w=300
right now I'm dealing with it by doing
$file = substr($media,0, strrpos($image, '.') + 4);
I'm just wondering whether it's a good practice or there's a better way.
I've tried pathinfo and a couple other methods, but all return extension with the query string.
Thanks
Parse the URL with parse_url, retrieve the path part:
$datum = parse_url($url);
$parts = pathinfo($datum['path']);
$ext = $parts['extension'];
You may also use getImageInfo($full_url), if fopen_wrappers allow it, and retrieve image info such as width, height, and most importantly, mime_type.
This because you will find several files without extension or with the wrong one, put there to trick browsers into downloading as image and trusting that the browser will recognize the image format nonetheless (been there, done that :-( )
I'm unsure whether you mean you want the extension or (judging from your current code) the full path (minus any query string).
Here's both:
$file = "http://site-name/images/dude-i-m-batman.jpg?1414151413";
preg_match('/^([^\?]+)(?:\?.*)?/', $file, $path_noQS);
preg_match('/(?<=\.)(\w{2,5})(?:\?.*)?/', $file, $extension);
echo $path_noQS[1]; //path, without QS
echo $extension[1]; //extension
Obviously what you do now has some shortcomings. One of them you already noticed your own:
Not all URLs end with the file-extension.
Not all file-extensions are of three letters (e.g. .jpeg)
So what you want is to get the path from a URL:
$imagePath = parse_url($imageUrl, PHP_URL_PATH);
And then you want to get the extension from that path:
$imageName = pathinfo($imagePath, PATHINFO_EXTENSION);
And done. You're not the first who needs that, so functions already exist for the job.
Your solution only works with 3 character extensions. If you know all the extensions will be 3 characters than yours is a perfectly viable solution. Otherwise:
$ext = pathinfo($filename, PATHINFO_EXTENSION);
This should definitely work if you have the correct file name
If for some reason that doesn't work, you can use this:
$ext = end(explode('.', $filename));
$ext = substr(strrchr($filename, '.'), 1);
$ext = substr($filename, strrpos($filename, '.') + 1);
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$ext = $exts[$n];
may be something like this
$parsedUrl = parse_url('http://site-name/images/dude-i-m-batman.jpg?1414151413');
$parsedFileInfo = pathinfo($parsedUrl['path']);
echo $parsedFileInfo['extension'];
http://codepad.org/KXZwKCjs
$u = 'http://site-name/images/dude-i-m-batman.zip.jpg?1414151413?1234';
$u = explode('?', $u, 2 ); // ignore everything after the first question mark
$ext = end(explode('.',$u[0])); // last 'extension'

Categories