function file_handle_upload($prefix, $subtype, $plugin)
{
    $desc = get_input("description");
    $tags = get_input("tags");
    $tags = explode(",", $tags);
    $folder = get_input("folder_text");
    if (!$folder) {
        $folder = get_input("folder_select");
    }
    $access_id = (int) get_input("access_id");
    $container_guid = (int) get_input('container_guid', 0);
    if (!$container_guid) {
        $container_guid == get_loggedin_userid();
    }
    // Extract file from, save to default filestore (for now)
    // see if a plugin has set a quota for this user
    $file_quota = trigger_plugin_hook("{$plugin}:quotacheck", 'user', array('container_guid' => $container_guid));
    if (!$file_quota) {
        // no, see if there is a generic quota set
        $file_quota = get_plugin_setting('quota', $plugin);
    }
    if ($file_quota) {
        // convert to megabytes
        $file_quota = $file_quota * 1000 * 1024;
    }
    // handle uploaded files
    $number_of_files = get_input('number_of_files', 0);
    $quota_exceeded = false;
    $bad_mime_type = false;
    for ($i = 0; $i < $number_of_files; $i++) {
        $title = get_input("title_" . $i);
        $uploaded = $_FILES["upload_" . $i];
        if (!$uploaded || !$uploaded['name']) {
            // no such file, so skip it
            continue;
        }
        if ($plugin == "photo") {
            // do a mime type test
            if (in_array($uploaded['type'], array('image/jpeg', 'image/gif', 'image/png', 'image/jpg', 'image/jpe', 'image/pjpeg', 'image/x-png'))) {
                $file = new PhotoPluginFile();
            } else {
                $bad_mime_type = true;
                break;
            }
        } else {
            $file = new FilePluginFile();
        }
        $dir_size = $file->getFilestoreSize($prefix, $container_guid);
        $filestorename = strtolower(time() . $uploaded['name']);
        $file->setFilename($prefix . $filestorename);
        $file->setMimeType($uploaded['type']);
        $file->originalfilename = $uploaded['name'];
        $file->subtype = $subtype;
        $file->access_id = $access_id;
        $uf = get_uploaded_file('upload_' . $i);
        if ($file_quota) {
            $file_size = strlen($uf);
            if ($dir_size + $file_size > $file_quota) {
                $quota_exceeded = true;
            }
        }
        if (!$quota_exceeded) {
            // all clear, so try to save the data
            $file->open("write");
            $file->write($uf);
            $file->close();
            $file->title = $title;
            $file->description = $desc;
            if ($container_guid) {
                $file->container_guid = $container_guid;
            }
            // Save tags
            $file->tags = $tags;
            $file->simpletype = file_get_general_file_type($uploaded['type']);
            $file->folder = $folder;
            $result = $file->save();
            if ($result) {
                // Generate thumbnail (if image)
                if (substr_count($file->getMimeType(), 'image/')) {
                    $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
                    $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true);
                    $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false);
                    if ($thumbnail) {
                        $thumb = new ElggFile();
                        $thumb->setMimeType($uploaded['type']);
                        $thumb->setFilename($prefix . "thumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumbnail);
                        $thumb->close();
                        $file->thumbnail = $prefix . "thumb" . $filestorename;
                        $thumb->setFilename($prefix . "smallthumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumbsmall);
                        $thumb->close();
                        $file->smallthumb = $prefix . "smallthumb" . $filestorename;
                        $thumb->setFilename($prefix . "largethumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumblarge);
                        $thumb->close();
                        $file->largethumb = $prefix . "largethumb" . $filestorename;
                    }
                }
                // add to this user's file folders
                file_add_to_folders($folder, $container_guid, $plugin);
                add_to_river("river/object/{$plugin}/create", 'create', $_SESSION['user']->guid, $file->guid);
            } else {
                break;
            }
        } else {
            break;
        }
    }
    if ($quota_exceeded) {
        echo elgg_echo("{$plugin}:quotaexceeded");
    } else {
        if ($bad_mime_type) {
            echo elgg_echo("{$plugin}:badmimetype");
        } else {
            if ($result) {
                if ($number_of_files > 1) {
                    echo elgg_echo("{$plugin}:saved_multi");
                } else {
                    echo elgg_echo("{$plugin}:saved");
                }
            } else {
                if ($number_of_files > 1) {
                    echo elgg_echo("{$plugin}:uploadfailed_multi");
                } else {
                    echo elgg_echo("{$plugin}:uploadfailed");
                }
            }
        }
    }
}