I am using PHP for a website. I have to get users upload their pics. Now, the pics can be in jpeg or png or gif format. My problem is that i identify a user by a unique userid, so I want to save the pics as: userid.jpg or userid.png etc. Now, how do i find out the extension of the file selected by the user using php?
我在网站上使用PHP。我必须让用户上传他们的照片。现在,照片可以是jpeg或png或gif格式。我的问题是我通过唯一的用户ID识别用户,所以我想将图片保存为:userid.jpg或userid.png等。现在,我如何找到用户使用php选择的文件的扩展名?
9 个解决方案
#1
I wouldn't rely on the file extension provided by the visitor. Instead, ask the file itself what it is (this can still be spoofed, but its less likely; also you don't have to wonder if the extension is 3 or 4 characters (jpeg, jpg for example)). Try:
我不会依赖访问者提供的文件扩展名。相反,问问文件本身是什么(这仍然是欺骗,但它不太可能;你也不必怀疑扩展名是3或4个字符(例如jpeg,jpg))。尝试:
if ($_FILES['avatar']['type'] == "image/gif")
if ($_FILES['avatar']['type'] == "image/jpeg")
if ($_FILES['avatar']['type'] == "image/png")
They're much more reliable.
它们更可靠。
#2
Instead of relying on the extension sent by the client, you can also determine the type of the file, using fileinfo (PHP >= 5.3), or if you're using an older version of PHP, something like mime_content_type
您可以使用fileinfo(PHP> = 5.3),或者如果您使用的是旧版本的PHP,例如mime_content_type,而不是依赖客户端发送的扩展,也可以确定文件的类型。
Once you have the mime-type of the file, it's just a matter of mapping (like 'image/png' => 'png')
一旦你有文件的mime类型,它只是一个映射的问题(如'image / png'=>'png')
(Should actually be better for security reasons ; if I remember correctly, $_FILES['...']['type'] is sent by the client, and can be faked)
(出于安全原因,实际上应该更好;如果我没记错的话,$ _FILES ['...'] ['type']由客户端发送,并且可以伪造)
#3
If you're running a nicely updated server with PHP 5.3, you should look into the new File Information extension. It'll look at the files themselves and try to determine the mime type, from which you can get the extension. This is more reliable than looking at the extension or mime type as provided by the browser, as both of these can be faked.
如果你使用PHP 5.3运行一个很好的更新服务器,你应该查看新的文件信息扩展。它将查看文件本身并尝试确定mime类型,从中可以获得扩展名。这比查看浏览器提供的扩展或mime类型更可靠,因为这两者都可以伪造。
An ideal solution could use a combination of all of these methods for maximum effectiveness.
理想的解决方案可以结合使用所有这些方法,以获得最大的效果。
#4
$filename = "harbl.jpg";
$fileinfo = pathinfo($filename);
echo($fileinfo['extension']); // prints "jpg"
#5
Assuming you have
假设你有
<input type="file" name="foo">
You can do
你可以做
$info = pathinfo($_FILES['foo']['name']);
$extension = $info['extension'];
#6
array_pop(explode(".", $_FILES["image"]["name"]))
or
pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION)
#7
I've used Array in application that I've build recently. And it works very well.
我在最近构建的应用程序中使用了Array。而且效果很好。
$file_type = array('image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/pjpeg' => 'jpg',
'image/bmp' => 'jpg',
'image/png' => 'png');
if(array_key_exists(($_FILES['file_upload']['type']), $file_type){
Your Function
}
#8
$extension = end(explode(".",$_FILES['foo']['name']));
#9
Hai
You can check the image type easily in php.For example, you may have
<input type="file" name="image">
$img_name=$_FILES["image"]["name"]; //name of the image
$img_tmpname=$_FILES["image"]["tmp_name"]; //temporary name
$len=strlen($img_name); //length of image name
$dot=strpos($img_name,"."); //finding position of .
$type=substr($img_name,$dot,$len); //finding string from . to end of string.
Then use if statement and check the file
if($type=='.gif'||$type=='.jpg'||$type=='.jpeg'||$type=='.png'||$type=='.GIF'||$type=='.JPG'||$type=='.PNG'||$type=='.JPEG') {
//your code............. }
//你的代码.............}
#1
I wouldn't rely on the file extension provided by the visitor. Instead, ask the file itself what it is (this can still be spoofed, but its less likely; also you don't have to wonder if the extension is 3 or 4 characters (jpeg, jpg for example)). Try:
我不会依赖访问者提供的文件扩展名。相反,问问文件本身是什么(这仍然是欺骗,但它不太可能;你也不必怀疑扩展名是3或4个字符(例如jpeg,jpg))。尝试:
if ($_FILES['avatar']['type'] == "image/gif")
if ($_FILES['avatar']['type'] == "image/jpeg")
if ($_FILES['avatar']['type'] == "image/png")
They're much more reliable.
它们更可靠。
#2
Instead of relying on the extension sent by the client, you can also determine the type of the file, using fileinfo (PHP >= 5.3), or if you're using an older version of PHP, something like mime_content_type
您可以使用fileinfo(PHP> = 5.3),或者如果您使用的是旧版本的PHP,例如mime_content_type,而不是依赖客户端发送的扩展,也可以确定文件的类型。
Once you have the mime-type of the file, it's just a matter of mapping (like 'image/png' => 'png')
一旦你有文件的mime类型,它只是一个映射的问题(如'image / png'=>'png')
(Should actually be better for security reasons ; if I remember correctly, $_FILES['...']['type'] is sent by the client, and can be faked)
(出于安全原因,实际上应该更好;如果我没记错的话,$ _FILES ['...'] ['type']由客户端发送,并且可以伪造)
#3
If you're running a nicely updated server with PHP 5.3, you should look into the new File Information extension. It'll look at the files themselves and try to determine the mime type, from which you can get the extension. This is more reliable than looking at the extension or mime type as provided by the browser, as both of these can be faked.
如果你使用PHP 5.3运行一个很好的更新服务器,你应该查看新的文件信息扩展。它将查看文件本身并尝试确定mime类型,从中可以获得扩展名。这比查看浏览器提供的扩展或mime类型更可靠,因为这两者都可以伪造。
An ideal solution could use a combination of all of these methods for maximum effectiveness.
理想的解决方案可以结合使用所有这些方法,以获得最大的效果。
#4
$filename = "harbl.jpg";
$fileinfo = pathinfo($filename);
echo($fileinfo['extension']); // prints "jpg"
#5
Assuming you have
假设你有
<input type="file" name="foo">
You can do
你可以做
$info = pathinfo($_FILES['foo']['name']);
$extension = $info['extension'];
#6
array_pop(explode(".", $_FILES["image"]["name"]))
or
pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION)
#7
I've used Array in application that I've build recently. And it works very well.
我在最近构建的应用程序中使用了Array。而且效果很好。
$file_type = array('image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/pjpeg' => 'jpg',
'image/bmp' => 'jpg',
'image/png' => 'png');
if(array_key_exists(($_FILES['file_upload']['type']), $file_type){
Your Function
}
#8
$extension = end(explode(".",$_FILES['foo']['name']));
#9
Hai
You can check the image type easily in php.For example, you may have
<input type="file" name="image">
$img_name=$_FILES["image"]["name"]; //name of the image
$img_tmpname=$_FILES["image"]["tmp_name"]; //temporary name
$len=strlen($img_name); //length of image name
$dot=strpos($img_name,"."); //finding position of .
$type=substr($img_name,$dot,$len); //finding string from . to end of string.
Then use if statement and check the file
if($type=='.gif'||$type=='.jpg'||$type=='.jpeg'||$type=='.png'||$type=='.GIF'||$type=='.JPG'||$type=='.PNG'||$type=='.JPEG') {
//your code............. }
//你的代码.............}