How can I make sure that the supplied file - font(TrueType)? File path may be specified with custom extension(non .ttf).
I'm confused.
Check this: finfo
For example:
$fileName = 'file.ext';
$mimeTypes = array('font/ttf','font/truetype');
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $fileName);
if(in_array($mime, $mimeTypes)){
echo 'font file';
}
finfo_close($finfo);
You must check FILEINFO_MIME_TYPE.
Related
function upload(&$model){
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$file = $_FILES['file'];
$file_name = $file['name'];
$file_ext2 = explode('.', $file_name);
$file_extenstion = strtolower(end($file_ext2));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo(finfo_file($finfo, $file['tmp_name']));
echo($file['type']);
}
return "superliga_view";
}
Why
echo(finfo_file($finfo, $file['tmp_name']));
prints image/jpeg, but
echo($file['type']);
prints "image/png"?
I try make watemark, but
$image = imagecreatefrompng($target);
sometimes throws errors due to wrong file type. With small files php always reads them as png, but with large files it reads them as jpeg.
I'm trying to get mime type of the profile picture they're in format user_id.jpg or gif or png, I experimented with this code but it's not working.
function detectFileMimeType($filename='')
{
$filename = escapeshellcmd($filename);
$command = "file -b --mime-type -m /usr/share/misc/magic {$filename}";
$mimeType = shell_exec($command);
return trim($mimeType);
}
function get_avatar($image, $user_id, $account)
{
$imgurl ="http://mypage/files/pictures/picture-" . ($user_id, $mimeType) . ".jpg";
if (!is_imgurl_good($imgurl)) {
$imgurl = "http://mypage/sites/all/themes/simple_custom/user.png";
}
return $imgurl;
}
You should be able to get the MIME type of a file fairly easily with built in functions provided by PHP -
function detectFileMimeType($filename='')
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type
$fileglob = glob($filename);
echo finfo_file($finfo, $fileglob);
finfo_close($finfo);
}
http://php.net/manual/en/function.finfo-file.php
http://php.net/manual/en/function.glob.php
As well as finfo which works fine, a slightly easier method is to get PHP getimagesize function return, as this will return the MIME type as extracted from the file:
$file = "blah/blahblah/bubbles.jpg";
$fileTypeData = getimagesize($file);
$fileTypeData['mime'] = The Mime type of the image file.
Here is the file upload code. It works in such a way that it accepts all the image extensions. But it needs to validate the file type (video, word doc etc). I need it to only upload images. For an example what happens now is that when I select a word document and submit my form, it shows a bunch of errors, inserts the record but not the file. What should happen is that, if the file is anything other than an image, it should not let the user insert the record. Should get an error message saying to check the file type when the form is submitted. Please assist me in achieving this.
if( isset($_FILES['img']) )
{
//resizing the image
$image = new SimpleImage();
$image->load($_FILES['img']['tmp_name']);
$image->resizeToHeight(180);
$info = pathinfo($_FILES['img']['name']);
$file = 'uploads/' . basename($_FILES['img']['name'],'.'.$info['extension']) . '.png';
if ($image->save($file))
{
if($fp = fopen($file , 'rb'))
{
$data = fread($fp, filesize($file));
//encoding the the image only to text so can be stored in DB
$data = base64_encode($data);
fclose($fp);
}
}
else
{
$error = '<p id="failed">Invalid Image</p>';
}
You need to check MIME type on the image, something like that:
if (isset($_FILES['img'])) {
$file = $_FILES['img'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
if (strpos($mime, 'image') === false) {
die('The submitted file is not an image!');
}
// Uploading code..
}
If mime string has 'image' in it, then it's image. Hope that will help.
In older PHP versions you can use mime_content_type. However if you have PHP > 5.3 you should use the finfo_* functions
You should also check is_uploaded_file() rather than isset()
if( is_uploaded_file( $_FILES['img']['tmp_name'] ) ) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file( $_FILES['img']['tmp_name'] );
if( $type == 'image/gif' ) { // for example
// do stuff
}
}
$filename=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$extension=strtolower(substr($filename, strpos($filename, '.')+1));
$size=$_FILES['file']['size'];
if(($extension=='jpg' || $extension=='jpeg') && ($type!='image/jpg' || $type!='image/jpeg')){...
I have a input file, can let user upload jpg/jpeg image only, I have check type, extension, size.
However I'm not sure how to check if user change extension.(ex. abc.php -> abc.jpg)
any thing else I need to check before I save user's image into my server?
You can check the image with exif_imagetype()
http://www.php.net/manual/en/function.exif-imagetype.php
exif_imagetype() reads the first bytes of an image and checks its
signature.
I would suggest using finfo:
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
/* outputs:
text/html
image/gif
application/vnd.ms-excel
*/
?>
example taken from php document site.
see more info on the php document page http://www.php.net/manual/en/function.finfo-file.php
#Fabian's answer looks good for checking the type of file. While I would suggest a different approach to getting the extension of the file.
Consider a file named stack.overflow.jpg.
$filename = 'stack.overflow.jpg';
// With your code $extension = 'overflow.jpg'
$extension=strtolower( substr( $filename, strpos( $filename, '.' ) +1 ) );
// With pathinfo() $extension = 'jpg'
$extension = pathinfo( $filename, PATHINFO_EXTENSION );
Consider using pathinfo() to get the file extension: http://www.php.net/manual/en/function.pathinfo.php
how to check whether file is image or video type in php version 5.2.9
$mime = mime_content_type($file);
if(strstr($mime, "video/")){
// this code for video
}else if(strstr($mime, "image/")){
// this code for image
}
Should work for most file extentions.
See my answer to
How can I check if a file is a mp3 or image file?
Example Code
function getMimeType($filename)
{
$mimetype = false;
if(function_exists('finfo_fopen')) {
// open with FileInfo
} elseif(function_exists('getimagesize')) {
// open with GD
} elseif(function_exists('exif_imagetype')) {
// open with EXIF
} elseif(function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
}
return $mimetype;
}
I use the following code which IMO is more universal than in the first and the most upvoted answer:
$mimeType = mime_content_type($filename);
$fileType = explode('/', $mimeType)[0];
I hope it was helpful for anyone.
You can check the MIME type using the finfo_file function
Example from the help page
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?>
EDIT: after better checking your question, this won't work, finfo functions require PHP 5.3.0
if(isset($_FILES['my_file'])) {
$mime = $_FILES['my_file']['type'];
if(strstr($mime, "video/")){
$filetype = "video";
}else if(strstr($mime, "image/")){
$filetype = "image";
}else if(strstr($mime, "audio/")){
$filetype = "audio";
}
Rather old question, but for others looking at this in the future, I would handle this like so:
function getType($file): string
{
$mime_type = mime_content_type($file);
return strtok($mime_type, '/');
}
This method utilises strtok to return the portion of the $mime_type string before the first /.
For example, let's say $file has a $mime_type of video/mp4, the getType method will return video.
I use this code and it works very well.
$mimeType = $request->images->getMimeType();
$fileType = explode('/', $mimeType)[0];
if it was an image, this code will give you the image word in the $fileType and if it was a video this code will give you the video word in the $fileType, then you can check on it by the if conditions.
good luck