You are here

function webfm_upload in Web File Manager 5

Same name and namespace in other branches
  1. 5.2 webfm.module \webfm_upload()

Called by upload form submit

1 string reference to 'webfm_upload'
webfm_menu in ./webfm.module
Implementation of hook_menu().

File

./webfm.module, line 959

Code

function webfm_upload() {
  global $user;

  //Get the destination path from the edit-webfmuploadpath hidden field in the upload form
  $json_data = array();
  $fid = '';
  if ($_POST['webfmuploadpath']) {
    $root_dir = $user->uid == 1 || user_access('administer webfm') ? file_directory_path() : file_directory_path() . webfm_get_root_path();
    $dest = $root_dir . $_POST['webfmuploadpath'];

    // Save new file uploads to tmp dir.
    if (($file = file_check_upload('webfm_upload')) != FALSE) {

      // Scale image uploads.
      _webfm_image($file);
      if (webfm_upload_validate($file, $err) === TRUE) {

        // file has been put in temp and we have a valid file object
        // Cache filepath as $_SESSION var for ajax confirm when name conflict
        // positive response will swap fid and munged name
        $upload_path = $dest . '/' . $file->filename;
        if (is_file($upload_path)) {
          $file->in_db = ($webfm_file_row = webfm_get_file_record('', $upload_path)) ? TRUE : FALSE;
          $file->dest = $dest;
          $_SESSION['temp_upload'] = $file;
          $json_data['file'] = $file->filename;
          if ($file->in_db) {

            // Overwrite of an existing file that is in the database
            // Only admins or file owners can overwrite
            if ($webfm_file_row->uid == $user->uid || user_access('administer webfm')) {
              $json_data['html'] = webfm_reload_upload('webfm/upload', webfm_version_form($file->filename));
            }
            else {
              drupal_set_message(t('Permission denied to overwrite existing file'), error);
            }
          }
          else {

            // Overwrite of an existing file that is not in the database
            $msg = '';
            if (($fid = webfm_version_upload(WEBFM_REPLACE_RENAME, $msg)) !== FALSE) {

              // file was inserted into the database
              drupal_set_message(t('Upload Success'));
            }
            else {
              drupal_set_message(t('Insertion into database fail'), error);
            }
          }
        }
        else {
          if (file_move($file, $dest)) {

            // file was moved to its final destination
            // Insert file into database
            if (($fid = webfm_dbinsert_file($file, $err)) !== FALSE) {
              drupal_set_message(t('Upload Success'));
            }
            else {
              file_delete($file->filepath);
              drupal_set_message(t('Insertion into database fail'), error);
            }
          }
          else {
            drupal_set_message(t('file_move to %path failed', array(
              '%path' => $dest,
            )), error);
          }
        }
      }
      else {
        drupal_set_message(t('file %s is not valid for upload', array(
          '%s' => $file->filename,
        )), error);
      }
    }
    else {
      if (!isset($_FILES['files']) || $_FILES['files']['name']['webfm_upload'] == '') {
        drupal_set_message(t('Please click "Browse" and select a file to upload before clicking the upload button.'), error);
      }
    }
  }
  else {
    drupal_set_message(t('Invalid upload path'), error);
  }
  if (!isset($json_data['html'])) {
    $json_data['html'] = webfm_reload_upload('webfm/upload');
  }
  if ($fid) {
    $json_data['fid'] = $fid;
  }
  print drupal_to_js(array(
    'status' => TRUE,
    'data' => $json_data,
  ));
  exit;
}