duanba3707 2014-03-13 08:55
浏览 92
已采纳

PHP MIME类型,限制上传文件

I have this code

if(isset($_FILES['uploadfile']))   
    {  
    $maxsize    = 10485760; //10MBx1024
    $acceptable = array(
        'image/jpeg',       
        'image/jpg',
        'image/gif',
        'image/png',
        'application/pdf',
        'text/plain',
        'application/vnd.ms-excel',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'application/vnd.ms-word',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'application/zip',
        'application/x-zip-compressed',
        'application/x-compressed',
        );

and this is the following parts:

if((!in_array($_FILES['uploadfile']['type'], $acceptable)) && (!empty($_FILES["uploadfile"]["type"]))) 
{
$msg= "<b><img src=redwarn.png'><font color='red'> This file is not accepted.</b></font>";  

goto cancel; 
}

The above code could limit the extension that user upload, but I tried to upload a IMAGE file, it come out an error that the file wasn't accepted, but I did add 'image/jpeg' in the array, is there any error in my code?

Thanks

  • 写回答

1条回答 默认 最新

  • doupi7619 2014-03-13 09:18
    关注

    Replace your if condition which is following

    if ($_FILES["uploadfile"]["error"] > 0){
          //add your code here
         //$maxsize=..
         //$acceptable_array=...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?