检查php中的filesize图像上传

时间:2015-01-24 15:18:16

标签: php upload filesize

上传图片时出现问题最大3M或4M返回成功上传,但他不是真的在文件夹中上传我想正确显示消息错误。 我认为检查文件大小的问题

        $dir_name=dirname(__FILE__)."/../admin/upload/";
        $path=@$_FILES['file_upload']['tmp_name'];
        $name=@$_FILES['file_upload']['name'];
        $fsize=$_FILES['file_upload']['size'];
        $multiext= array("gif","jpeg","jpg","png");
        $erros=array();

                $extention = strtolower(end(explode(".", $name)));
                $extcheck = in_array($extention,$multiext);
                $newname=rand(1,9999999999).'.'.$extention;

                if( !$extcheck )
                {
                    $erros[] = "<div class='msgerrorup'>Invalid file</div>";
                }

                    if( ($fsize > 100000) )
                    {
                        $erros[] =  "<div class='msgerrorup'>Image size must be 100k</div>";

                    }

                    if(!$newname )
                    {

                        $erros[]  = "<div class='seccessup'>Invalid parametrs</div>";

                    }

    if ( empty($erros) )
    {
       move_uploaded_file($path,$dir_name.$newname);  
        echo "<div class='seccessup'>Success upload</div>";
    }
    else
    {
        foreach ($erros as $erro)
        {
          echo $erro."<br />";
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个

    $path=@$_FILES['file_upload']['tmp_name'];
    $name=@$_FILES['file_upload']['name'];
    $fsize=$_FILES['file_upload']['size'];
    $ftype=$_FILES['file_upload']['type'];
    $multiext= array("image/gif","image/jpeg","image/jpg","image/png");
    $erros=array();
      if( !in_array($ftype, $multiext) )
         {
            $erros[] = "<div class='msgerrorup'>Invalid file</div>";
         }

以及

  if ( count($erros) == 0 )
    {
       move_uploaded_file($path,$dir_name.$newname);  
        echo "<div class='seccessup'>Success upload</div>";
    }