限制文件上传类型 [英] Limiting file upload type

查看:92
本文介绍了限制文件上传类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题.有没有一种方法只能在上传时允许txt文件?我环顾四周,我发现的只是text/php,它允许使用PHP.

Simple question. Is there a way to only allow txt files upon uploading? I've looked around and all I find is text/php, which allows PHP.

$uploaded_type=="text/php

推荐答案

使用PHP上传文件时,该文件存储在$ _FILES数组中.在其中有一个名为"type"的密钥,它具有文件EG $_FILES['file']['type']

When you upload a file with PHP its stored in the $_FILES array. Within this there is a key called "type" which has the mime type of the file EG $_FILES['file']['type']

所以要检查它是否是txt文件

So to check it is a txt file you do

if($_FILES['file']['type'] == 'text/plain'){
    //Do stuff with it.
}

此处进行了很好的解释.另外,不要依赖文件扩展名,这是非常不可靠的.

It's explained very well here. Also, don't rely on file extentions it's very unreliable.

这篇关于限制文件上传类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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