php file upload, how to restrict file upload type

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

    Here is some code I wrote in the past..

    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
    }
    

提交回复
热议问题