I am sure someone knows how to do this

This script is now working however I get error messages from empty uploads?

There are two possible files and two possible error messages. This has the potential of producing four possible error messages.

If I upload, two files (0 and 1) and both of them pass both tests – no error message.

If I upload only one file (0) and it passes both tests – two error messages, because the empty file number (1) that I did not choose to upload produces a false bool on both $sizeok and $typeok as it passes through the script.

How do I prevent the error messages from an empty upload? Thanks.

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

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

  foreach ($_FILES['image']['name'] as $number => $file) {
	
      $filenm = str_replace(' ', '_', $file);
	  
	  $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;       
          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:
          }

		  if (!$typeok) {
		    $result[] = "$filenm unknown type";
			}
			if ($typeok) {
				move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
				}
				$ndfilenm = UPLOAD_DIR . $filenm;
			
          //if ($success)
		  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>
        <input name="upload" type="submit" id="upload" value="Upload files" />
    </p>
</form>

Thank you so much. That seems to have worked.

I did not know that it could be placed in the script like the other if statements that grab the size, name, tmp_name etc. (if ($_FILES[‘image’][‘size’][$number])). And the word continue; I had no idea.

You want to ignore the upload if error == UPLOAD_ERR_NO_FILE;


if($_FILES['image']['error'][0] == UPLOAD_ERR_NO_FILE) {
  //ignore. No upload attempted
}

http://www.php.net/manual/en/features.file-upload.errors.php

Well nothing really goes in there. The point is that if no upload was attempted you don’t do anything with that upload slot.

What about something like this:


foreach ($_FILES['image']['name'] as $number => $file) { 
   if($_FILES['image']['error'][$number] == UPLOAD_ERR_NO_FILE) {
       continue;
    }

  //rest of your script...
}

continue is for loops. It quits the current loop iteration and resumes with the next.


$nums = array(1, 2, 3, 4);

foreach($nums as $n) {
  if($n == 2) {
     continue;
  }
  echo $n . ' ';
}

That will output 1 3 4

Thanks for your response.

I read that part of the manual.

Could you elaborate on where this code might go and what exactly it might say in the brackets. I really have no idea.