forms - 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" />

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(
  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."

  $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; 

    $errors[] = 'Set a correct mail address.';

    $errors[] = 'Maximum 2MB file size.';

    $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!


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




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

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


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

People are also looking for solutions to the problem: php - Send XML with SOAP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.