Can I check whether or not a certain file is an image? How can do this in PHP?
我可以检查某个文件是否是图像吗?怎么能用PHP做到这一点?
If the file is not an image, I want to give an alert message.
如果文件不是图像,我想给出一条警告信息。
5 个解决方案
#1
5
In addition to getimagesize()
, you can use exif_imagetype()
除了getimagesize()之外,你还可以使用exif_imagetype()
exif_imagetype() reads the first bytes of an image and checks its signature.
exif_imagetype()读取图像的第一个字节并检查其签名。
When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.
找到正确的签名后,将返回适当的常量值,否则返回值为FALSE。返回值与getimagesize()在索引2中返回的值相同,但exif_imagetype()的速度要快得多。
For both functions, FALSE is returned if the file is not determined to be an image.
对于这两个函数,如果文件未确定为图像,则返回FALSE。
#2
1
in PHP you can do it like following way
在PHP中你可以像下面这样做
if ((($_FILES['profile_picture']['type'] == 'image/gif') || ($_FILES['profile_picture']['type'] == 'image/jpeg') || ($_FILES['profile_picture']['type'] == 'image/png')))
in Javascript You can do it like following way
在Javascript中您可以像下面这样做
function checkFile() {
var filename = document.getElementById("upload_file").value;
var ext = getExt(filename);
// alert(filename.files[0].filesize);
// alert(ext);
if(ext == "gif" || ext == "jpg" || ext=="png")
return true;
alert("Please upload .gif, .jpg and .png files only.");
document.getElementById("upload_file").value='';
return false;
}
function getExt(filename) {
var dot_pos = filename.lastIndexOf(".");
if(dot_pos == -1)
return "";
return filename.substr(dot_pos+1).toLowerCase();
}
#3
1
In php
we can use filetype ( string $filename )
and mime_content_type ( string $filename )
在php中我们可以使用filetype(string $ filename)和mime_content_type(string $ filename)
but mime_content_type ( string $filename )
is deprecated
但不推荐使用mime_content_type(string $ filename)
http://php.net/manual/en/function.filetype.php
http://php.net/manual/en/function.filetype.php
In javascript
we can use custom functions
在javascript中我们可以使用自定义函数
http://my-sliit.blogspot.in/2009/04/how-to-check-upload-file-extension.html
http://my-sliit.blogspot.in/2009/04/how-to-check-upload-file-extension.html
#4
0
I would do this to find out...
我会这样做才能找出......
$type =array('jpg','gif');
foreach($type as $val){
if($_FILES['filename']['type'] == 'image/$val')
{
echo "its an image file";
}
else{
echo "invalid image file"
}
#5
0
Better and faster way is to use exif_imagetype(). Something like that should do the work:
更好更快的方法是使用exif_imagetype()。这样的事情应该做的工作:
$valid_formats = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
$file_format = exif_imagetype($filename);
if(!in_array($file_format, $valid_formats))
echo("File format is not valid");
#1
5
In addition to getimagesize()
, you can use exif_imagetype()
除了getimagesize()之外,你还可以使用exif_imagetype()
exif_imagetype() reads the first bytes of an image and checks its signature.
exif_imagetype()读取图像的第一个字节并检查其签名。
When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.
找到正确的签名后,将返回适当的常量值,否则返回值为FALSE。返回值与getimagesize()在索引2中返回的值相同,但exif_imagetype()的速度要快得多。
For both functions, FALSE is returned if the file is not determined to be an image.
对于这两个函数,如果文件未确定为图像,则返回FALSE。
#2
1
in PHP you can do it like following way
在PHP中你可以像下面这样做
if ((($_FILES['profile_picture']['type'] == 'image/gif') || ($_FILES['profile_picture']['type'] == 'image/jpeg') || ($_FILES['profile_picture']['type'] == 'image/png')))
in Javascript You can do it like following way
在Javascript中您可以像下面这样做
function checkFile() {
var filename = document.getElementById("upload_file").value;
var ext = getExt(filename);
// alert(filename.files[0].filesize);
// alert(ext);
if(ext == "gif" || ext == "jpg" || ext=="png")
return true;
alert("Please upload .gif, .jpg and .png files only.");
document.getElementById("upload_file").value='';
return false;
}
function getExt(filename) {
var dot_pos = filename.lastIndexOf(".");
if(dot_pos == -1)
return "";
return filename.substr(dot_pos+1).toLowerCase();
}
#3
1
In php
we can use filetype ( string $filename )
and mime_content_type ( string $filename )
在php中我们可以使用filetype(string $ filename)和mime_content_type(string $ filename)
but mime_content_type ( string $filename )
is deprecated
但不推荐使用mime_content_type(string $ filename)
http://php.net/manual/en/function.filetype.php
http://php.net/manual/en/function.filetype.php
In javascript
we can use custom functions
在javascript中我们可以使用自定义函数
http://my-sliit.blogspot.in/2009/04/how-to-check-upload-file-extension.html
http://my-sliit.blogspot.in/2009/04/how-to-check-upload-file-extension.html
#4
0
I would do this to find out...
我会这样做才能找出......
$type =array('jpg','gif');
foreach($type as $val){
if($_FILES['filename']['type'] == 'image/$val')
{
echo "its an image file";
}
else{
echo "invalid image file"
}
#5
0
Better and faster way is to use exif_imagetype(). Something like that should do the work:
更好更快的方法是使用exif_imagetype()。这样的事情应该做的工作:
$valid_formats = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
$file_format = exif_imagetype($filename);
if(!in_array($file_format, $valid_formats))
echo("File format is not valid");