You are here

function imce_upload_submit in IMCE 6

Same name and namespace in other branches
  1. 6.2 inc/imce.page.inc \imce_upload_submit()
  2. 7 inc/imce.page.inc \imce_upload_submit()

Submit upload form.

1 string reference to 'imce_upload_submit'
imce_upload_form in inc/page.inc
Upload form.

File

inc/page.inc, line 261

Code

function imce_upload_submit($form, &$form_state) {
  $form_state['redirect'] = FALSE;
  $imce =& $form['#parameters'][2]['imce'];
  $validators = array(
    'imce_validate_all' => array(
      &$imce,
    ),
  );
  $dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']);

  //save uploaded file.
  $replace = variable_get('imce_settings_replace', FILE_EXISTS_RENAME);
  if ($file = file_save_upload('imce', $validators, $dirpath, $replace)) {

    //core bug #203204.
    @chmod($file->filepath, 0664);

    //core bug #54223.
    if ($replace == FILE_EXISTS_RENAME) {
      $name = basename($file->filepath);
      if ($name != $file->filename) {
        $file->filename = $name;
        drupal_set_message(t('The file is renamed to %filename.', array(
          '%filename' => $file->filename,
        )));
      }
    }
    else {
      if ($replace == FILE_EXISTS_REPLACE) {

        //check duplicates
        if ($_file = db_fetch_object(db_query("SELECT fid FROM {files} WHERE filepath = '%s' AND fid <> %d", $file->filepath, $file->fid))) {
          db_query("DELETE FROM {files} WHERE fid = %d", $file->fid);
          $file->fid = $_file->fid;
        }
      }
    }
    $file->uid = $imce['uid'];

    //global user may not be the owner.
    $file->status = FILE_STATUS_PERMANENT;

    //make permanent
    drupal_write_record('files', $file, array(
      'fid',
    ));

    //update
    drupal_set_message(t('%filename is uploaded.', array(
      '%filename' => $file->filename,
    )));

    //update file list
    $img = imce_image_info($file->filepath);
    $file->width = $img ? $img['width'] : 0;
    $file->height = $img ? $img['height'] : 0;
    imce_add_file($file, $imce);

    //create thumbnails
    if (isset($form_state['values']['thumbnails']) && $img) {
      imce_create_thumbnails($file->filename, $imce, $form_state['values']['thumbnails']);
    }
  }
  else {
    drupal_set_message(t('Upload failed.'), 'error');
  }
}