PHP switch and error Code

if (isset($_POST['upload'])) {
	switch ($_FILES['filename']['error']) {
		case 0:
			$message = $_FILES['filename']['name'] . ' was uploaded successfully.';
		case 2:
			$message = $_FILES['filename']['name'] . ' is too big to upload.';
		case 4:
			$message = 'No file selected.';
			$message = 'Sorry, there was a problem uploading ' .$_FILES['filename']['name'];
`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?

Your code is not quite complete: and really should use the defined constants instead of numeric values.

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

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.

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.

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

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.

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"; 
        case UPLOAD_ERR_FORM_SIZE: 
            $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
        case UPLOAD_ERR_PARTIAL: 
            $message = "The uploaded file was only partially uploaded"; 
        case UPLOAD_ERR_NO_FILE: 
            $message = "No file was uploaded"; 
        case UPLOAD_ERR_NO_TMP_DIR: 
            $message = "Missing a temporary folder"; 
        case UPLOAD_ERR_CANT_WRITE: 
            $message = "Failed to write file to disk"; 
            $message = "File upload stopped by extension"; 

            $message = "Unknown upload error"; 
    return $message; 

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.