php file upload, how to restrict file upload type

前端 未结 5 1183
小蘑菇
小蘑菇 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:33

    This may be useful:

    First check desired mime types to verify:

    Microsoft Office MIME Types and List of MIME Types

    Then try make your code easier...

        $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 !
        }
    

    Important: User may change file extension, so always check the mime type intead of extension!! =)

提交回复
热议问题