PHP switch and error Code


#1
if (isset($_POST['upload'])) {
	switch ($_FILES['filename']['error']) {
		case 0:
			$message = $_FILES['filename']['name'] . ' was uploaded successfully.';
			break;
		case 2:
			$message = $_FILES['filename']['name'] . ' is too big to upload.';
			break;
		case 4:
			$message = 'No file selected.';
			break;
		default:
			$message = 'Sorry, there was a problem uploading ' .$_FILES['filename']['name'];
			break;
	}
}
?>
`switch ($_FILES['filename']['error']) {`

This is fetching error → ['error'] form the array, but how come those switch case: 0, 2, 4 are associating with those errors. Does errors also happens in even numbers?


#2

Your code is not quite complete: http://www.php.net/manual/en/features.file-upload.errors.php and really should use the defined constants instead of numeric values.


#3

Thanks, but the question was more about the connectivity of switch cases with the errors.


#4

I presume you meant to ask whether they happen in “odd” numbers. I don’t know how this specific code works, but sometimes errors or status codes are stored using a bitmap, so a single byte can store eight different error codes, with each bit being set to indicate that specific status is true. In this case it’s unlikely that the error could be both too big and not selected, but if there were a combination of allowed status codes, then you would use a bitwise AND to extract each code. That you don’t need to here implies that multiple statuses are not possible here.

ETA: I stress sometimes - as @ahundiak pointed out there are odd error codes, this clearly isn’t a bitmap.


#5

Correct. 1,2,3,4,6,7,8 are all valid file upload error codes. Never been good at understanding poetry. No idea what the actual question is.


#6

case 0, case 2, case 4 how are they connecting to the actual error codes such as 1,2,3,4,5,6 …?
01_05.zip (2.1 KB)


#7

Ah, then if that’s the case my bitmap explanation is not the case here. If it was a bitmap, then an error of ‘3’ would suggest that errors ‘2’ and ‘1’ are true, not have an entirely different meaning as it does in this case.

case 0 deals with the situation where the error code is 0 (i.e. success, no error), case 2 deals with error code 2, and case 4 deals with error code 4. You don’t deal with any of the other error codes.


#8

In other words, this function copied from the original link you were given is what a complete switch statement would look like:

function codeToMessage($code) 
{ 
    switch ($code) { 
        case UPLOAD_ERR_INI_SIZE: 
            $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini"; 
            break; 
        case UPLOAD_ERR_FORM_SIZE: 
            $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
            break; 
        case UPLOAD_ERR_PARTIAL: 
            $message = "The uploaded file was only partially uploaded"; 
            break; 
        case UPLOAD_ERR_NO_FILE: 
            $message = "No file was uploaded"; 
            break; 
        case UPLOAD_ERR_NO_TMP_DIR: 
            $message = "Missing a temporary folder"; 
            break; 
        case UPLOAD_ERR_CANT_WRITE: 
            $message = "Failed to write file to disk"; 
            break; 
        case UPLOAD_ERR_EXTENSION: 
            $message = "File upload stopped by extension"; 
            break; 

        default: 
            $message = "Unknown upload error"; 
            break; 
    } 
    return $message; 
}