2011-09-06 142 views
1

我有以下代码以检查是否(简历和参考字母上传匹配期望类型(PDF或文档或DOCX)和大小(小于400 KB)PHP文件上传,如何限制文件上传类型

//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 } 

根据需要这不是工作,甚至允许txt文件通过+大小限制没有被选中,有什么不好呢?

感谢,

+2

OMG,你应该重写它,而不是通过创建允许的MIME类型的列表中找到:-)开始的bug和文件结尾,然后核对这个列表... – home

回答

4

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

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.... 

    } 

    } 

} 
+1

我知道这个帖子是从去年一年,但我遇到了同样的问题。我尝试了上面的答案,但是我的doc和pdf测试文件没有通过safari和chrome(没有在ie或ff中测试过)。这两个文件都低于上述脚本中指定的400kb。我去了链接,并得到了正确的mimes:'application/msword(doc)|应用程序/ pdf(pdf)|和text/plain(txt)'。我拿出的唯一其他东西是参考部分。 –

+1

无法让它工作,所以我修改了一下为我工作: '//将允许的MIME类型文件添加到允许的数组中--endline-- $ allowed = array('doc ','docx','txt','pdf'); --endline-- //检查上传的文件类型是否在上面的数组中(因此有效)--endline-- if(in_array(pathinfo($ _ FILES ['resume'] ['name'],PATHINFO_EXTENSION), $ allowed)){--endline - ' –

+0

尽管这篇文章非常陈旧,但如果像我这样的人发现它试图获得帮助,我会添加相关信息。 $ _FILES ['whatever'] ['type']值包含引号,例如它将是'“application/pdf”'。这些引用将需要删除,以便比较匹配'in_array'。例如,'in_array(str_replace(''','',$ _ FILES ['whatever'] ['type'],$ allowed))' – vertigoelectric

0

的MIME类型docxapplication/vnd.openxmlformatsofficedocument.wordprocessingml.document

0

下面是一些代码,我过去写的..

function checkFileExtension($ext) 
{ 
    if ($ext == 'ai' || $ext == 'pdf' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 
     'gif' || $ext == 'eps' || $ext == 'tif' || $ext == 'png' || $ext == 'xls' || $ext == 
     'xlsx' || $ext == 'doc' || $ext == 'docx' || $ext == 'ppt' || $ext == 'pptx' || 
     $ext == 'zip' || $ext == 'rar' || $ext == 'sitx' || $ext == 'psd' || $ext == 
     'indd' || $ext == 'dng') { 
     $pass = (int)1; 
    } else { 
     $pass = (int)0; 
    } 
    return (int)$pass; 
} 


$ext = substr(strrchr($_FILES['file']['name'], "."), 1); 
$fileAccepted = checkFileExtension($ext); 
$fileSize = $_FILES['file']['size']; 

if($fileAccepted==1 && $fileSize > '82428800'){ 
    // do stuff 
} 
0

要做到这一点,我通常使用类似的东西:

$filename = $_FILES['field_name']['name']; // Get the name of the file (including file extension). 
$ext = strtolower(substr($filename, strpos($filename,'.'), strlen($filename)-1)); //get the extention in lower case 

而不是检查文件的扩展名被接受。

另外要注意的是,用户可以简单地改变扩展了危险的文件,因此它是安全检查的MIME类型

0

这可能是有用的:

首先检查所需的MIME类型验证:

Microsoft Office MIME TypesList of MIME Types

然后尽量使你的代码更容易...

$mimeTypes = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
'application/vnd.openxmlformats-officedocument.presentationml.presentation'); 

    if (in_array($_FILES["resume"]["type"], $mimeTypes)) 
    { 
     // File's OK 
    } 
    else 
    { 
     // Bad file ! 
    } 

重要提示:用户可能会更改文件扩展名,因此请务必检查扩展名的MIME类型intead! =)