You are here

function filebrowser_form_rename_submit in Filebrowser 7.3

Same name and namespace in other branches
  1. 7.4 filebrowser.module \filebrowser_form_rename_submit()

Rename files.

File

./filebrowser.module, line 1000

Code

function filebrowser_form_rename_submit($form, &$form_state) {

  // Original and new names.
  $original_files = $form_state['values']['original_files'];
  $new_names = $form_state['values']['new_names'];

  // Scan each original file to see if it has been changed
  foreach ($original_files as $fid => $original_file) {

    // Test if filename was changed.
    if (!empty($new_names[$fid]) && $original_file['display-name'] != $new_names[$fid]) {
      $new_file = array(
        'display-name' => $new_names[$fid],
        'full-path' => $original_file['path'] . '/' . $new_names[$fid],
      );

      // Test if original file still exists.
      if (file_exists($original_file['full-path'])) {

        // Load original metadata.
        $metadata = module_invoke_all('filebrowser_metadata_get', $original_file);

        // Rename the file.
        rename($original_file['full-path'], $new_file['full-path']);

        // Save metadata to the new filename.
        module_invoke_all('filebrowser_metadata_set', $new_file, $metadata);

        // Clear the old one.
        module_invoke_all('filebrowser_metadata_set', $original_file, NULL);
      }
    }
  }
}