Error if empty form submitted in PHP

I'm trying to make function like this. Which can separate the newfilename so it can be input in database. But this code only works if the form was full filled, I try to submit the empty form but there is an error.

Warning: Illegal offset type in C:\xampp\htdocs\cobas\create.php on line 38

Warning: Illegal offset type in C:\xampp\htdocs\cobas\create.php on line 42

Notice: Undefined offset: 1 in C:\xampp\htdocs\cobas\create.php on line 45

Notice: Undefined offset: 1 in C:\xampp\htdocs\cobas\create.php on line 47

Notice: Undefined index: in C:\xampp\htdocs\cobas\create.php on line 47

Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in C:\xampp\htdocs\cobas\create.php on line 47

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpE539.tmp' to 'upload/' in C:\xampp\htdocs\cobas\create.php on line 47

And this is the code:

$sql = "INSERT INTO employees (name, address, salary, code, photo, photo2) VALUES (?, ?, ?, ? , ?, ?)";
mysqli_stmt_bind_param($stmt, "ssssss", $param_name, $param_address, $param_salary, $asd, $new_filename[0] , $new_filename[1]);

define( 'MAX_ALLOWED_FILE_SIZE', 5 * 1024 * 1024 ); // 5Mb
define( 'UPLOAD_PATH', 'upload' );

function is_allowed_file_ext( $filename = null )
{
    $allowed_ext = array("jpg", "jpeg", "gif", "png");
    $_parts = explode( '.', $filename );
    $_ext = end( $_parts );
    $_ext = strtolower( $_ext );
    if( in_array( $_ext , $allowed_ext ) ){
        return $_ext;
    }
    return false;
}

if( !empty( $_FILES['photo'] ) ){
    foreach ($_FILES['photo']['name'] as $key[] => $filename ) {
        if( $_FILES['photo']['error'][$key] == UPLOAD_ERR_OK ){                                     // line 38

            if( $file_ext = is_allowed_file_ext( $filename ) ){
                $new_filename[] = sprintf( '%d-%s.%s', round(microtime(true)), uniqid(), $file_ext );
                if( $_FILES['photo']['size'][$key] <= MAX_ALLOWED_FILE_SIZE ){                      //line 42
                    // Everything is okay now, save the file                   
                    $dest_path[0] = sprintf( '%s/%s', rtrim(UPLOAD_PATH,'/'), $new_filename[0] );
                    $dest_path[1] = sprintf( '%s/%s', rtrim(UPLOAD_PATH,'/'), $new_filename[1] );   //line 45                 
                    move_uploaded_file( $_FILES['photo']['tmp_name'][$key[0]], $dest_path[0] );
                    move_uploaded_file( $_FILES['photo']['tmp_name'][$key[1]], $dest_path[1] );     //line 47

                }else{
                    $photo_errors[] = 'Error: File size is larger than the allowed limit.';
                }
            }else{
                $photo_errors[] = $filename . ' is not a valid image';
            }
        }else{
            switch ( $_FILES['photo']['error'][$key] ) {
                case UPLOAD_ERR_NO_FILE:
                    $photo_errors[] = 'No file sent';
                    break;
                case UPLOAD_ERR_INI_SIZE:
                case UPLOAD_ERR_FORM_SIZE:
                    $photo_errors[] = 'Max file size exceeded!';
                    break;
                default:
                    $photo_errors[] = 'Unknown errors';
                    break;
            }
        }
    }
}else{
    $photo_errors[] = 'No photos selected';
}

1 answer