开发者

php drops file type error for file size

Fellas, I've run in an interesting bug in my code and I can't figure out the reason.

I have a form with 3 fields. Name, email, file upload fields. After the fields are filled it sends the stuff back to the same file.

<form enctype="multipart/form-data" method="POST" action="index.php">
    <input type="text" name="name" value="<?php if(isset($_POST['name'])&&!empty($_POST['name'])){ echo $_POST['name'];}?>" maxlength="100" />
    <input type="text" name="email" value="<?php if(isset($_POST['email'])&&!empty($_POST['email'])){ echo $_POST['email'];}?>" maxlength="50" />
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" /> <input type="file" name="file_upload" size="15" value="<?php if(isset($_FILES["file_upload"]["name"])&&!empty($_FILES["file_upload"]["name"])){ echo $_FILES["file_upload"]["name"];}?>"/>
    <input type="submit" name="submit" value="" id="image-button" />
</form>

So the form sent back to the index.php. Now the error handling: (the values of the error fields are in Hungarian, but I translate the necessary fields)

$upload_errors = array(
// http://www.php.net/manual/en/features.file-upload.errors.php
  UPLOAD_ERR_OK         => "Nincsenek hibák.",
  UPLOAD_ERR_INI_SIZE   => "Nagyobb a megengedett fileméretnél.",
  UPLOAD_ERR_FORM_SIZE  => "Nagyobb a form megengedett fileméreténél.",
  UPLOAD_ERR_PARTIAL    => "Részleges feltöltés.",
  UPLOAD_ERR_NO_FILE    => "File nem található.",
  UPLOAD_ERR_NO_TMP_DIR => "Átmeneti könyvtár nem található.",
  UPLOAD_ERR_CANT_WRITE => "Nem írható cél mappa.",
  UPLOAD_ERR_EXTENSION  => "Hibás kiterjesztés."
);



if(isset($_POST['submit'])){
  $errors = array();

  $required_fields = array('name' => 'A név megadása kötelező.', 'email' => 'E-mail cím megadása kötelező.' );
  foreach($required_fields as $key => $value) {
    if (!isset($_POST[$key]) || empty($_POST[$key])) { 
        $errors[] = $value; 
    }
  }

if(!is_valid_email($_POST['email'])){
    $errors[] = 'Set a correct mail address.';
    }

if($_FILES["file_uplo开发者_如何学编程ad"]["size"]>2097152){
    $errors[] = 'Maximum 2MB file size.';
    }   

if(!isset($_FILES["file_upload"]["name"])||empty($_FILES["file_upload"]["name"])){
    $errors[] = 'You must upload a picture!';
    }   

if (($_FILES["file_upload"]["type"] !== "image/png")&& ($_FILES["file_upload"]["type"] !== "image/jpg") && ($_FILES["file_upload"]["type"] !== "image/jpeg") && ($_FILES["file_upload"]["type"] !== "image/pjpeg")){
    $errors[] = 'Only PNG or JPG/JPEG files allowed.';
    }

When I try to upload a file with a file size 2MB+ the error returns:

  • Only PNG or JPG/JPEG files allowed. -

This error belongs to the file type! The form validation returns a different error!

Question:

Why is this happen? Why don't triggers the Maximum 2MB file size. error???


If a file is bigger than upload_max_filesize[docs] (which is usually 2MB), PHP will ignore the file, set ['size'] to 0 and ['error'] to UPLOAD_ERR_INI_SIZE.

Check ['error'] instead of ['size'].

See http://docs.php.net/manual/en/features.file-upload.post-method.php

BTW you should not rely on ["type"], it's set by the client, and the client can set it to anything he wants.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜