When > MAX_FILE_SIZE I get unknown type error

This script can upload 3 files.

First it checks for MAX_FILE_SIZE.
Second it checks that the file is an image of the right type.
Third it reduces the image size.

It was giving unknown type errors for the empty slots, that is if I upload just one or two files instead of all three, the slot would cause $typeok to remain false. That caused the error. Now there is an error suppressor for the empty slots near the top of the script. That suppressed the unknown type errors and for empty slots.

The problem I am having now is if the image size is greater than max, but the image type is good, I get two errors instead of one.

  1. ScreenShot010.jpg size is too large
  2. ScreenShot010.jpg unknown type

Notice how they are numbered 1 and 2 instead of 0 and 1. I am not sure if this is correct. maybe it’s a clue.

Can someone help me understand why I still get unknown type errors and what to do about it. Thanks.

<?php
define('MAX_FILE_SIZE', 120000);
define('UPLOAD_DIR', 'upload_test/');

if (array_key_exists('upload', $_POST)) {

  foreach ($_FILES['image']['name'] as $number => $file) {
  
	////////// If less than 3 files uploaded, ////////////////
	//////stop (unknown file error) for the empty slots //////
	if($_FILES['image']['error'][$number] == UPLOAD_ERR_NO_FILE) {
       continue;
	}
	///////////////////////////////////////////////////////////
	
    $filenm = str_replace(' ', '_', $file);
	 
	////// Check file size //////
	$sizeok = false;
	
	if ($_FILES['image']['size'][$number] > 0 && $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
      $sizeok = true;
	}
	
	if (!$sizeok) {
		$result[] = "$filenm size is too large";
	}
	/////////////////////////////
         
	$udfilenm = $_FILES['image']['tmp_name'][$number];
		 
    $typeok = false; 
	
	////// Check file type- image gif, jpeg or png //////
    switch ($_FILES['image']['type'][$number]) {
		case "image/gif":
		$src = imagecreatefromgif($udfilenm); $typeok = true; break;
				   
		case "image/jpeg":
		case "image/pjpeg":
		$src = imagecreatefromjpeg($udfilenm); $typeok = true; break;
				    
		case "image/png":
		$src = imagecreatefrompng($udfilenm); $typeok = true; break;
				     
		default:
    }//var_dump($typeok);
	
	if ($typeok) {
		move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
		$ndfilenm = UPLOAD_DIR . $filenm;
	}
	else { 
		$result[] = "$filenm unknown type";
	}
	////////////////////////////////////////////////////
	
	////// Reduce image size //////		
	if ($sizeok && $typeok) {
		list($w, $h) = getimagesize($ndfilenm);
			  
        $max = 100;
        $tw = $w;
        $th = $h;             
        if ($w > $h && $max < $w) {
            $th = $max / $w * $h;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h) {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w) {
            $tw = $th = $max;
        }              
        $tmp = imagecreatetruecolor($tw, $th);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp, array(// Sharpen image 
        array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
        imagejpeg($tmp, $ndfilenm);
        imagedestroy($tmp);
        imagedestroy($src);
    }
  }
}
?>

<?php
if (isset($result)) {
  echo '<ol>';
  foreach ($result as $item) {
    echo "<strong><li>$item</li></strong>";
	}
  echo '</ol>';
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="multiUpload" id="multiUpload">
    <p>
        <label for="image1">File 1:</label>
		<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="image[]" id="image1" />
    </p>
    <p>
        <label for="image2">File 2:</label>
        <input type="file" name="image[]" id="image2" />
    </p>
	<p>
        <label for="image3">File 3:</label>
        <input type="file" name="image[]" id="image3" />
    </p>
    <p>
        <input name="upload" type="submit" id="upload" value="Upload files" />
    </p>
</form>

" if (!$sizeok) {" = " else {".

I’d split the three lines of the case statement onto seperate lines, just for readability.

There is no break in your default case. (not necessary, but a good practice anyway)

What did the var_dump output $typeok as? Presumably False.
What do you get when you var_dump($_FILES[‘image’][‘type’][$number]) ?

Why not put the $result = “$filenm unknown type”; line as the Default case?

The numbers are coming from the fact you wrapped your error output in <ol> (Ordered List), and should start at 1.

But … the ‘type’ key of the array returned by [fphp]getimagesize/fphp ?

You need to check the error message. If Error is greater then zero skip.
Second you should not be using the “type” parameter.

I don’t know how this post got by me. I posted this, waited over 24 hours and got no response, so I put this technical problem aside and moved on thinking no one was willing to help. Just now I discover you guys came to help. I thank you very much for your responses. I am over whelmed with your answers because I am new at this. I will study your suggestions and refer back to them when I return to this problem. Thanks again.

That or exif_imagetype. Of course the OP is already using getimagesize too.