$upload = new vB_Upload_Attachment($vbulletin);
        $image =& vB_Image::fetch_library($vbulletin);
        $upload->data =& $attachdata;
        $attachdata->set('visible', intval($vbulletin->GPC['newvisible']));
        $attachdata->set('counter', 0);
        $upload->image =& $image;
        $upload->postinfo = array('postid' => $attachment['postid']);
        $upload->userinfo = array('userid' => $attachment['userid'], 'attachmentpermissions' => $vbulletin->userinfo['attachmentpermissions'], 'forumpermissions' => $vbulletin->userinfo['forumpermissions']);
        $upload->emptyfile = false;
        if (!empty($vbulletin->GPC['url'])) {
            $attachment_input = $vbulletin->GPC['url'];
        } else {
            $attachment_input = array('name' => &$vbulletin->GPC['upload']['name'], 'tmp_name' => &$vbulletin->GPC['upload']['tmp_name'], 'error' => &$vbulletin->GPC['upload']['error'], 'size' => &$vbulletin->GPC['upload']['size']);
        }
        $vbulletin->options['allowduplicates'] = true;
        if (!$upload->process_upload($attachment_input)) {
            print_stop_message('generic_error_x', $upload->fetch_error());
        }
    } else {
        $attachdata->set('filename', $vbulletin->GPC['a_filename']);
        $attachdata->set('visible', intval($vbulletin->GPC['a_visible']));
        $attachdata->set('counter', $vbulletin->GPC['a_counter']);
        $attachdata->save();
    }
    define('CP_REDIRECT', 'attachment.php?do=stats');
    print_stop_message('updated_attachment_successfully');
}
// ###################### Delete an attachment ####################
if ($_REQUEST['do'] == 'delete') {
    $vbulletin->input->clean_array_gpc('r', array('attachmentid' => TYPE_INT));
    $attachment = $db->query_first("SELECT filename FROM " . TABLE_PREFIX . "attachment WHERE attachmentid=" . $vbulletin->GPC['attachmentid']);
     $upload->postinfo =& $postinfo;
     if ($vbulletin->GPC['attachment']['url']["{$x}"]) {
         $attachment =& $vbulletin->GPC['attachment']['name']["{$x}"];
     } else {
         $attachment = array('name' => &$vbulletin->GPC['attachment']['name']["{$x}"], 'tmp_name' => &$vbulletin->GPC['attachment']['tmp_name']["{$x}"], 'error' => &$vbulletin->GPC['attachment']['error']["{$x}"], 'size' => &$vbulletin->GPC['attachment']['size']["{$x}"]);
     }
     $attachcount++;
     if (!$foruminfo['allowposting']) {
         $error = $vbphrase['this_forum_is_not_accepting_new_attachments'];
         $errors[] = array('filename' => $attachment['name'], 'error' => $error);
     } else {
         if ($vbulletin->options['attachlimit'] and $attachcount > $vbulletin->options['attachlimit']) {
             $error = construct_phrase($vbphrase['you_may_only_attach_x_files_per_post'], $vbulletin->options['attachlimit']);
             $errors[] = array('filename' => $attachment['name'], 'error' => $error);
         } else {
             if ($attachmentid = $upload->process_upload($attachment)) {
                 if ($vbulletin->userinfo['userid'] != $postinfo['userid'] and can_moderate($threadinfo['forumid'], 'caneditposts')) {
                     $postinfo['attachmentid'] =& $attachmentid;
                     $postinfo['forumid'] =& $foruminfo['forumid'];
                     require_once DIR . '/includes/functions_log_error.php';
                     log_moderator_action($postinfo, 'attachment_uploaded');
                 }
             } else {
                 $attachcount--;
             }
             if ($error = $upload->fetch_error()) {
                 $errors[] = array('filename' => is_array($attachment) ? $attachment['name'] : $attachment, 'error' => $error);
             }
         }
     }
 }