Image url like below.
$url = https://lh3.googleusercontent.com/-4T303dPCnp0ZnkzQjSIjeB7k8L_DiQglhUgNqXM-OkeLQXoNwwFnzM1MoFjJGw7DGI=w300
How to get extension from this url.
I m using pathinfo to get extension but it's not work
$ext = pathinfo($destinationPath.$filename, PATHINFO_EXTENSION);
Anyone help me to get image extension from url.
pathinfo just parses a path, you can't use it in this context, when path doesn't have extension in it. You need to assign extension based on mime type.
So, first, get mime type, like here: https://stackoverflow.com/a/12023961/1059672
Then, assign extension based on mime type, like here: https://stackoverflow.com/a/16511092/1059672
Related
Im retrieving images from thee google places API on my PHP server by using the following lines of code
$response = file_get_contents($url);
I'm also writing this file onto a folder on the server like so: (The file returned is a binary file, not an image file)
file_put_contents($folderPath,$response);
How do I find out the file extension of the file I read into $response?
I have tried the following:
$response = file_get_contents($url);
$size = getimagesize($response);
$extension = image_type_to_extension($response)
Google wont return a file with a suffix. Try mime_content_type($response) and then parse the response to figue out the correct suffix
try pathinfo
http://php.net/manual/en/function.pathinfo.php
it should help you
[EDIT]
Since your image is returned as binary, then use php's getimagesizefromstring http://php.net/manual/en/function.getimagesizefromstring.php
It will return all the details you need.
You can use pathinfo to get the extension.
$extension = pathinfo($response, PATHINFO_EXTENSION);
I have a url like so:
$file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215";
It will either be an image or a video but it does not have the extension within the url.
How can I get the extension of this file from the url so that I can save it with the right extension?
$extension=?;
copy($file,"newname.$extension");
You can use
$file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215";
//copy file to server
$type= mime_content_type($file)
//rename file based on the type
output
image/gif
http://php.net/manual/en/function.mime-content-type.php
I found another solution for your information:
$extension=get_headers($file,1)["Content-Type"];
Will output image/gif before copy.
I am using move_file_upload on my server side in php so that client can allow users to upload their kml files to the server. I know that in order to check an image , in php I can use $check = getimagesize(file) but what would be the equivalent for a kml file check ?
I donot want to just check the extension of the file. I wish to know if infact the file is a valid kml file or not. If I only check the extension, someone can just post some other malicious file and change its extension to .kml
If you want to see if the file has the extension KML, you can use:
$filename = $_FILES["file"]["name"]; //or however you are getting the filename
$ext = end((explode(".",$filename)));
if($ext!="kml"){
//Extension is incorrect
}
Checking mime content can be helpful.
I am not quite sure what is the correct mime name of kml files but at least with checking in google it should be something as:
mime_content_type ($file) === 'application/vnd.google-earth.kml+xml'
How ever its possible that there are mimes set to 'application/xml' or 'text/xml' so extension validation is required as well ..
PHP: I am uploading image files in a folder with a name of particular format which is like postid_posttype_postserial.ext so that while showing particular blog post I would just use this format based on the post id, post type of that post. But problem here is I dont have extension of that file. So here is the actual question, how do I get the extension of a file of which I just know the name(in name.ext) not the extension.
FYI I searched for this and I found few function which returns extensions but you have to pass a full filename that is with extension to get the info about the file.
In your case, you need to get the mime type of the file then do a switch statement in PHP. You can get the mime type of the file by:
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mime_type = finfo_file($finfo, $postid_posttype_postserial_file);
finfo_close($finfo);
$mime_types = array(
'image/gif' => '.gif',
'image/jpeg' => '.jpg',
// etc.
);
$ext = array_key_exists($mime_type, $mime_types) ? $mime_types[$mime_type] : false;
echo $ext;
Learn more: http://www.php.net/manual/en/function.finfo-file.php
Update 2: As pointed out by the comment, replacing switch to array matching is more elegant.
I am trying to find out what extensions a particular url has, Here is what I am trying to do:
$pathinfo = pathinfo('http://imgur.com/9P54j');
$extension = $pathinfo['extension'];
echo $extension;
The url 'http://imgur.com/9P54j' is actually a url having 9P54j.gif image, and its not evident in the url, how do I extract the extension .gif of the the file '9P54j' ?
That URL is not a URL to the .gf image, but a page that contains the image in its HTML. You will want to parse the HTML for the URL to the image. Try: rightclick on the image in the link you provided above, and click "open image" or "view image" to see the full URL.
Even that URL may not have an extension because the data may be streamed to the user bia PHP. If that's the case, just check the Content-Type header to find out what the extension is.
You can use a regex to extract it, something like this:
$url = 'http://imgur.com/9P54j';
$content = file_get_contents($url);
$explode = explode('/', $url);
preg_match('/http:\/\/i\.imgur\.com\/' . array_pop($explode) . '(\.[A-z]{1,4})/', $content, $matches);
$ext = $matches[1]; // '.gif'
My answer assumes that would like to grab the file's extension from urls that have no extensions in the url itself.
Using pathinfo() will not work as it retrieves the extension using text procession and in the url there is just no extension.
An approach would be to use lower level http functionality that allows to send a http request to the url and fetch the response headers. The response headers should regulary contain the 'Content-Type:' header that shows us the mimetype of the content.
Once having the 'Content-Type' header you could use a translation table and translation mimetype to file extension. This list of supported extensions would of course be limited und there are mimetypes that can translate to more than one extension. In such cases you would have to do further investigations on the files content itself.
As a real php programm would be too large for this answer I'll give just a pseudo code:
<?php
function get_extension_from_mimetype($url) {
// static translation table. to be extended
static $translationTable = array (
'image/jpeg' => 'jpg',
'text/xml' => 'xml',
'text/html' => 'html'
);
$defaultExtension = 'dat';
// you'll have to write this method
$mimetype = get_mimetype_by_url($url);
if(isset($translationTable[$mimetype])) {
$extension = $translationTable[$mimetype];
} else {
$extension = $defaultExtension;
}
return $extension;
}