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.
Related
Am trying to download an image from remote server and save it in local folder but i cannot seem to find a solution here. i have seen a couple of similar questions around but mine is a bit unique.
Solutions here are for remote links ending in image name and extension e.g www.example.com/image.jpg mine is a plain url but links to an image whose image type and name is unknown e.g. https://example.com/images/6a9b0547-3d31-4dc9-891c-3ae6ec051056. please help.
Well, at first you have to download the file to a temporary place. Let's assume you save the file as 399443553.tmp. Use mime_content_type() function to get the mime type of the file. At last, move the file to its real location with the extension you got from mime_content_type()
Here is the documentation for mime_content_type()
Example from php.net
<?php
echo mime_content_type('php.gif') . "\n";
?>
output
image/gif
PS. Don't be confused about the .gif in the filename. It will return the real MIME type even if it is not there
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 ..
I have added in mime file:
'mp4' => array('video/mp4', 'video/3gpp'),
still not able to upload files. Not showing any errors also.
Any idea how to upload all types of files in codeigniter through form
Hi just add your mime type to
application/config/mimes
'mp4' =>'video/mp4'
'3gp'=>'video/3gpp'
then just add each one individually in the allowed types
Link for all the mime types
Make sure you are doing the right thing by checking the extension and the file type as well! For instance, if your file name is video.mp4, your file type is probably video/mp4 but not necessarily! (I was just dealing with a .m4a file and my audio type is audio/mp4.)
You can test the correct file type by printing it out for yourself after an upload.
echo $_FILES[0]['file_type'];
After that, you can add the following line to the mimes.php config file:
$mimes['<your extension>'] = '<your file type>';
Alternatively, if you encounter multiple MIME types for the same extension, you can add an array instead:
$mimes['<your extension>'] = array('<your file type 1>', '<your file type 2>');
I hope this helps!
While uploading file I am getting mime-type as application/octet-stream.
I have come to know that
Zend_Frameworks tries to determine the mimetype in two ways:
First it tries to use the PECL FILEINFO-Extension (which is not installed on every server)
if the extension is not istalled it tries to use mime_content_type (a php function). This function however is deprecated as of php version 5.3
So what to do now? How can I be sure that user uploading a file is image only and not something else? How can I detect mime type of uploaded file?
For images you can also rely on exif_imagetype but I would recommend that you install finfo.
See this for an example implementation.
Use Zend_File_Transfer validators to exlude types that you dont want :
$upload = new Zend_File_Transfer();
// Does not allow MIME type application/pdf and application/zip .
$upload->addValidator('ExcludeMimeType', false, array('application/pdf',
'application/zip'));
or you can also use IsImage validator which checks if a transferred file is a image file :
$upload = new Zend_File_Transfer();
// Checks whether the uploaded file is a image file
$upload->addValidator('IsImage', false);