While user uploading a file, is it possible to know if the uploaded file is an image or not,
I am open for any solution, Client Side, Server Side or both and we choose based on the case.
While user uploading a file, is it possible to know if the uploaded file is an image or not,
I am open for any solution, Client Side, Server Side or both and we choose based on the case.
May be you can use following code to check if the file is an image.
public bool IsFileAnImage(string filePath)
{
try
{
Image image = Image.FromFile(filePath))
}
catch
{
return false;
}
finally
{
image.Dispose();
}
return true;
}
This can be done server side with GD by using the function below: ((modified version of this)
function imageFileTypeFromBinary($file)
{
$binary = file_get_contents($file);
if (
!preg_match(
'/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/',
$binary, $hits
)
) {
return 'application/octet-stream';
//return false;
}
static $type = array (
1 => '.jpeg',
2 => '.gif',
3 => '.png',
4 => '.x-windows-bmp',
5 => '.tiff',
6 => '.x-ilbm',
);
return $type[count($hits) - 1];
// or simply return true
// return true;
}
should you wish, you can make it return true, or false as opposed to the mime type.
A simple way would be to look at the file's extension.