get filename with pure extension - php

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'

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.

rename file name using path info extension with dot

i need new name of file name using pathinfo($url, PATHINFO_EXTENSION);
this my code
$name = "name.txt";
$f = fopen($name, 'r');
$nwname = fgets($f);
fclose($f);
$newfname = $destination_folder .$nwname. pathinfo($url, PATHINFO_EXTENSION);
output:
1 jpeg
how to make output nospace and write (.) dot before jpeg like this
output:
1.jpeg
thank
Solved in comments, here's write up.
The . is used for concatenation. So $variable.$variable puts the values of the two variables together. $variable.'.'.$variable would add a period between the 2 variables. The trim function should be used to remove leading and trailing whitespaces from a variable.
Functional demo: https://eval.in/520038
References:
http://php.net/manual/en/function.trim.php
http://php.net/manual/en/language.operators.string.php
I think you need to concatenate strings like below :
$newfname = $destination_folder .$nwname.'.'. pathinfo($url, PATHINFO_EXTENSION);

How to avoid file names with spaces and/or special characters

I have a web form to upload pictures from the users.
Then I am creating an iOS app to show the pictures loaded from the users. But the app is not loading the pictures if the file name contains spaces or special characters (like á, é, í, ó, ú, ñ, etc.), most of the users are from Spain...
This is the code I am using:
<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"] == "form1")) {
$randomString = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1) . substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10);
echo $randomString;
$nombre_archivo = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "logos/".$randomString.$nombre_archivo);
?>
I am using the random function to avoid repeated file names.
How could I change the file name given by the user and that may contain spaces and/or special characters, in a way that can be perfectly loaded in the iOS app?
rename the file after upload. this is an answer to OP's question in comments
// get the uploaded file's temp filename
$tmp_filename = $_FILES['userfile']['tmp_name'];
// get the file's extension
$path = $_FILES['userfile']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
// rename the uploaded file with a timestamp, insert the destination directory and add the extension
$new_filename = 'logos/'.date('Ymdhms').'.'.$ext;
// put the renamed file in the destination directory
move_uploaded_file($tmp_filename, $new_filename);
Edit: new answer per OP's question
<?php
if((isset($_POST["enviado"])) && ($_POST["enviado"] == "form1")) {
// get the uploaded file's temp filename
$tmp_filename = $_FILES['userfile']['tmp_name'];
// get the file's extension
$path = $_FILES['userfile']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
// rename the uploaded with a timestamp file, add the extension and assign the directory
$new_filename = 'logos/'.date('Ymdhms').'.'.$ext;
// put the renamed file in the destination directory
move_uploaded_file($tmp_filename, $new_filename);
}
Here is what you need to do.
1) generate a unique id based filename string.
2) Rename the file with newly generated filename.
<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>
If you actually wanted to keep the user-entered string as part of the file name, you could do something like this which transliterates UTF-8 characters to their ASCII equivalent (if possible) and then removes any non ASCII and invalid characters:
function get_file_name($string) {
// Transliterate non-ascii characters to ascii
$str = trim(strtolower($string));
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
// Do other search and replace
$searches = array(' ', '&', '/');
$replaces = array('-', 'and', '-');
$str = str_replace($searches, $replaces, $str);
// Make sure we don't have more than one dash together because that's ugly
$str = preg_replace("/(-{2,})/", "-", $str );
// Remove all invalid characters
$str = preg_replace("/[^A-Za-z0-9-]/", "", $str );
// Done!
return $str;
}
You could try combining this together with the unique ID for the file so if two users upload a file with the same file name they don't clash.

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

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

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

Categories