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>