php文件上传,如何限制文件上传类型 [英] php file upload, how to restrict file upload type

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

问题描述

我有以下的代码来检查(简历和参考信上传匹配所需的类型(pdf或doc或docx)和大小(小于400 kb)

<$ p $ b $ //检查文件扩展名和大小
$ resume =($ _FILES ['resume'] ['name']);
$ reference =($ _FILES [引用'] ['name']);
$ ext = strrchr($ resume,。);
$ ext1 = strrchr($ reference,。);
if !($ _ FILES [resume] [type] ==application / doc)
||($ _FILES [resume] [type] ==application / docx)
||($ _FILES [resume] [type] ==application / pdf))
&&(($ _FILES [reference] [type] ==application / doc)
||($ _FILES [reference] [type] ==application / docx)
||($ _FILES [reference] [ ($ ext ==.pdf)||($ ext ==.doc)||($ ext =.pdf)==application / pdf)
&& =.docx))
&&(($ ext1 ==.pdf)||($ ext1 ==.doc)||($ ext1 ==.docx))
&& ($ _FILES [resume] [size]< 400000)//接受高达500 kb
&& ($ _FILES [reference] [size]< 400000)){

stop user} else {允许上传文件}
pre>

这不是按照要求工作,甚至可以通过txt文件的大小限制来检查是不是正在检查,有什么问题呢?

感谢,

解决方案

下面只是使用mime类型来验证文件,然后检查两者的大小。有关大多数MIME类型的列表,请参阅此处或Google。

 函数allowed_file(){

//将允许的MIME类型的文件添加到'允许'array
$ allowed = array('application / doc','application / pdf','another / type');

//检查上传的文件类型是否在上面的数组中(因此有效)
if(in_array($ _ FILES ['resume'] ['type'],$ allowed)in_array $ _FILES ['reference'] ['type'],$ allowed)){

//如果找到文件类型允许的类型,继续检查文件大小:

if $ _FILES [resume] [size]< 400000 AND $ _FILES [reference] [size]< 400000){

//如果两个文件都小于给定的大小限制,允许上传
//开始filemove在这里....

}

}

}
code>


I have the following code to check if (resume and reference letter uploaded match desired type (pdf OR doc OR docx) and size (less than 400 kb)

//check file extension and size
         $resume= ($_FILES['resume']['name']); 
         $reference= ($_FILES['reference']['name']); 
         $ext = strrchr($resume, ".");
         $ext1 = strrchr($reference, ".");
        if (!(($_FILES["resume"]["type"] == "application/doc")
        || ($_FILES["resume"]["type"] == "application/docx")
        || ($_FILES["resume"]["type"] == "application/pdf" ))
         && (($_FILES["reference"]["type"] == "application/doc")
        || ($_FILES["reference"]["type"] == "application/docx")
        || ($_FILES["reference"]["type"] == "application/pdf"))
        && (($ext == ".pdf") || ($ext == ".doc") || ($ext == ".docx"))
        && (($ext1 == ".pdf") || ($ext1 == ".doc") || ($ext1 == ".docx"))
        &&  ($_FILES["resume"]["size"] < 400000) //accept upto 500 kb
        &&  ($_FILES["reference"]["size"] < 400000)) {  

stop user } else { allow files to upload }

This is not working as desired, allows even txt files through + the size limit is not being checked, what is wrong with it?

Thanks,

解决方案

The below just uses the mime types to validate a file, then checks the size of both. For a list of most mime types see here or google.

function allowed_file(){

//Add the allowed mime-type files to an 'allowed' array 
 $allowed = array('application/doc', 'application/pdf', 'another/type');

//Check uploaded file type is in the above array (therefore valid)  
    if(in_array($_FILES['resume']['type'], $allowed) AND in_array($_FILES['reference']['type'], $allowed)){

   //If filetypes allowed types are found, continue to check filesize:

  if($_FILES["resume"]["size"] < 400000 AND $_FILES["reference"]["size"] < 400000 ){

    //if both files are below given size limit, allow upload
    //Begin filemove here....

    }

    }

}

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

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