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

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=...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法