php文件上传,如何限制文件上传类型
发布时间:2020-12-13 21:28:06 所属栏目:PHP教程 来源:网络整理
导读:我有以下代码来检查(上传的简历和推荐信是否符合所需类型(pdf或doc或docx)和大小(小于400 kb) //check file extension and size $resume= ($_FILES['resume']['name']); $reference= ($_FILES['reference']['name']); $ext = strrchr($resume,"."); $ext1 =
我有以下代码来检查(上传的简历和推荐信是否符合所需类型(pdf或doc或docx)和大小(小于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 } 这是不能正常工作,允许甚至txt文件通过大小限制不被检查,它有什么问题? 谢谢, 解决方法
下面只使用mime类型来验证文件,然后检查两者的大小.有关大多数mime类型的列表,请参阅
here或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.... } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |