找到使用PHP上传文件的扩展名 [英] finding extension of the file being uploaded using php

查看:87
本文介绍了找到使用PHP上传文件的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP的网站。我必须让用户上传他们的照片。
现在,图片可以是JPEG或PNG或GIF格式。
我的问题是,我通过一个唯一的用户标识一个用户,所以我想保存的图片为:
userid.jpg或userid.png等
现在,我怎么找出用户使用php选择的文件的扩展名是什么?

解决方案

我不会依赖访问者提供的文件扩展名。相反,问文件本身是什么(这仍然可以被欺骗,但不太可能;也不必怀疑扩展名是3或4个字符(例如jpeg,jpg))。尝试:

$ $ p $ if($ _FILES ['avatar'] ['type'] ==image / gif)
if($ _FILES ['avatar'] ['type'] ==image / jpeg)
if($ _FILES ['avatar'] ['type'] ==image / png)

它们更可靠。


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?

解决方案

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:

if ($_FILES['avatar']['type'] == "image/gif")
if ($_FILES['avatar']['type'] == "image/jpeg")
if ($_FILES['avatar']['type'] == "image/png")

They're much more reliable.

这篇关于找到使用PHP上传文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆