1 file.pages.inc file_manage_form_submit($form, &$form_state)

Form submission handler for the 'Save' button for file_manage_form().

File

core/modules/file/file.pages.inc, line 650
Supports file operations including Manage and Delete.

Code

function file_manage_form_submit($form, &$form_state) {
  /* @var File $file */
  $file = $form_state['file'];
  /* @var File $temporary_upload */
  $temporary_upload = $form_state['values']['replace_upload'] ? file_load($form_state['values']['replace_upload']) : NULL;
  $orphaned_uri = '';

  // Check if a replacement file has been uploaded.
  if ($temporary_upload) {
    // If the extension is the same, install into the old file's location.
    if (pathinfo($temporary_upload->uri, PATHINFO_EXTENSION) == pathinfo($file->uri, PATHINFO_EXTENSION)) {
      file_unmanaged_move($temporary_upload->uri, $file->uri, FILE_EXISTS_REPLACE);
    }
    // If different extensions, copy the base file name but save the extension.
    else {
      $destination_uri = rtrim($file->uri, backdrop_basename($file->uri)) . backdrop_basename($temporary_upload->uri);
      $replace_mode = $destination_uri == $file->uri ? FILE_EXISTS_REPLACE : FILE_EXISTS_RENAME;
      if ($new_file_uri = file_unmanaged_move($temporary_upload->uri, $destination_uri, $replace_mode)) {
        // Update if the uri target has changed.
        if ($new_file_uri != $file->uri) {
          $orphaned_uri = $file->uri;
          $file->uri = $new_file_uri;
        }
      }
    }

    // Copy attributes from the temporary file to the original one.
    $file->filesize = $temporary_upload->filesize;
    $file->filemime = $temporary_upload->filemime;
    $file->timestamp = $temporary_upload->timestamp;

    // Clear any image styles for this file.
    $image_info = image_get_info($file->uri);
    if (!empty($image_info)) {
      image_path_flush($file->uri);
    }
  }

  // Run entity form submit handling and save the file.
  entity_form_submit_build_entity('file', $file, $form, $form_state);

  // A user might assign the associated user by entering a user name in the file
  // edit form, which we then need to translate to a user ID.
  if (isset($file->name)) {
    // The use of isset() is mandatory in the context of user IDs, because
    // user ID 0 denotes the anonymous user.
    if ($user = user_load_by_name($file->name)) {
      $file->uid = $user->uid;
    }
    else {
      // Anonymous user.
      $file->uid = 0;
    }
  }
  elseif ($file->uid) {
    $user = user_load($file->uid);
    $file->name = $user->name;
  }

  // Update the URI and copy the file if the schema has changed.
  if (file_uri_scheme($file->uri) != $form_state['values']['scheme']) {
    $file_destination = $form_state['values']['scheme'] . '://' . file_uri_target($file->uri);
    $file_destination = file_stream_wrapper_uri_normalize($file_destination);
    $file_destination_dirname = backdrop_dirname($file_destination);
    // Create the directory in case it doesn't exist.
    file_prepare_directory($file_destination_dirname, FILE_CREATE_DIRECTORY);
    if ($moved_file = file_move($file, $file_destination, FILE_EXISTS_RENAME)) {
      // Only re-assign the file object if file_move() did not fail.
      $file = $moved_file;
    }
  }

  // Save all the modifications to the file.
  $file->save();

  $args = array(
    '%title' => entity_label('file', $file),
  );
  watchdog('file', 'File: updated %title.', $args);
  backdrop_set_message(t('File: %title has been updated.', $args));

  // Delete the temporary file entity created during the upload.
  if (!empty($temporary_upload)) {
    $temporary_upload->delete();
  }

  // If a file was replaced and renamed, delete the old file left over.
  if (!empty($orphaned_uri)) {
    file_unmanaged_delete($orphaned_uri);
  }

  $form_state['redirect'] = 'admin/content/files';
}