donglv6960 2011-08-01 07:54
浏览 44
已采纳

文件类型PHP验证

What I am doing wrong with the code , it always shows 'File types .doc,.docx,.odt and max file size 2mb supported 'even if the file type is .doc format and size is 0kb.

if(($_FILES["file"]["type"] != 'application/octet-stream') ||
            ($_FILES["file"]["type"] != 'application/vnd.oasis.opendocument.text')||
            ($_FILES["file"]["type"] != 'application/msword')||
            ($_FILES["file"]["size"] > 200000))
        {
            //echo $_FILES["file"]["size"];
            //echo $_FILES["file"]["type"];
            $this->assign_values('msg',"File types .doc,.docx,.odt and max file size 2mb supported");
            //echo "Error: " . $_FILES["file"]["error"] . "<br />";
        }
  • 写回答

3条回答 默认 最新

  • drgbpq5930 2011-08-01 07:59
    关注

    You need to change the || to && between the file types. Or even better check the type against a list of allowed types:

    $allowedTypes = array('application/octet-stream', 'application/vnd.oasis.opendocument.text', 'application/msword');
    
    if(!in_array($_FILES["file"]["type"], $allowedTypes) || $_FILES["file"]["size"] > 200000)
            {
                //echo $_FILES["file"]["size"];
                //echo $_FILES["file"]["type"];
                $this->assign_values('msg',"File types .doc,.docx,.odt and max file size 2mb supported");
                //echo "Error: " . $_FILES["file"]["error"] . "<br />";
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序