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.
- ScreenShot010.jpg size is too large
- 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>