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);
Related
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
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 ..
like file upload there are
<?php
$_FILES['file']['tmp_name'];
$_FILES['file']['name'];
$_FILES['file']['size'];
$_FILES['file']['type'];
?>
now.
i have a file that is sitting on my other web server, and i want to get the name size and mime type of that file via url.. is this possible?..
i've alreay tried to use this code below. but it doesn't work
$url = "http://mydomain.com/myfile.rar";
filesize ( $url );
mime_content_type ( $url );
You can try native php function get_headers it's very fast way to read file data
You can't do it like this. The information you get when you use $_FILES is meta-information that is sent along with the file (and the size can even be calculated after the file is retrieved).
You cannot get this information like that, but you can download the actual file and inspect the header information to get that information. To do this, read about curl, which allows you to do HTTP requests to another server.
It might be possible to request just the headers, so you get the information without getting the file, which is obviously more efficient.
Another solution is to implement a file-info script on the other server that allows you to get the file info.
So you could request http://mydomain.com/fileinfo.php?file=myfile.rar. In fileinfo.php you can get all the file info of the given file and just echo it.
I am trying to get the real file type and for that I have come to PECL's finfo.
$fileName = "my.docx";
$f = finfo_open(FILEINFO_MIME_TYPE);
var_dump(finfo_file($f, $fileName)); // I get "application/zip"
How is this right? Is this what I was supposed to get?
The Word Microsoft Office Open XML Format Document format consists of a bunch of XML and other files stored in a zip file (unzip it and see). So yes, this is correct.
I want to create image from php using the function imagecreatefromjpeg(string $filename ),
but when I am providing a image URL as a parameter to this function then this function is not able to create image.
$pic = imagecreatefromjpeg('http://www.example.com/image.jpg');
header("Content-type: image/jpeg");
imagejpeg($pic);
imagedestroy($pic);
You can actually create images from remote files, but please be sure the 'fopen wrappers' have been enabled, see also http://php.net/manual/en/function.file.php
if it doesn't work, what kind of error do you see? and what kind of variable is $pic?
I can't check this right now, but i'd wager it has to be a local file. I.e. you need to have the file on your server.
php.net says: A URL can be used as a filename with this function if the fopen wrappers have been enabled. (http://php.net/manual/en/function.imagecreatefromjpeg.php)