php file upload, how to restrict file upload type

前端 未结 5 1192
小蘑菇
小蘑菇 2020-11-29 11:38

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          


        
5条回答
  •  感动是毒
    2020-11-29 12:35

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

提交回复
热议问题