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

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统