If and else statements on upload form!
Posted: Fri Dec 09, 2011 6:51 pm
The code works perfectly but I cannot get the following right:
- execute only if size is not okay but type is okay.
- execute only if type is not okay but size is okay.
It just skips down as if both type and size was negative, it doesn't check if it is either or. What am I doing wrong?
Here's the code:
- execute only if size is not okay but type is okay.
- execute only if type is not okay but size is okay.
It just skips down as if both type and size was negative, it doesn't check if it is either or. What am I doing wrong?
Here's the code:
Code: Select all
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0:
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
if ($success) {
$result = "$file uploaded successfully";
}
else {
$result = "Error uploading $file. Please try again.";
}
break;
case 3:
$result = "Error uploading $file. Please try again.";
default:
$result = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'] == 4) {
$result = 'No file selected';
}
// execute only if size is not okay but type is okay.
elseif (!$sizeOK && $typeOK) {
$result = "Maximum size allowed is $max. The file \"$file\" is too big.";
}
// execute only if type is not okay but size is okay.
elseif (!$typeOK && $sizeOK) {
$result = "Only file types: gif, jpg, and png are accepted.";
}
// execute only if both type and size is not okay. This works.
else {
$result = "The file \"$file\" is too big, the maximum size allowed is $max. <br /> The file also appears to be in a unsupported format, only file types: gif, jpg, and png are accepted.";
}
}