You are here

function _filefield_paths_file_move in File (Field) Paths 6.2

Move file and update its database record.

1 call to _filefield_paths_file_move()
filefield_paths_process_file in ./filefield_paths.module
Process FileField Paths settings, rename and move files.

File

./filefield_paths.module, line 251
Contains core functions for the FileField Paths module.

Code

function _filefield_paths_file_move($source, &$file) {

  // Backup messages.
  $messages = $_SESSION['messages'];

  // Create destination directories.
  $directories = array();
  foreach (explode('/', dirname($file['filepath'])) as $directory) {
    $directories[] = $directory;
    $path = file_create_path(implode($directories, '/'));
    if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
      watchdog('filefield_paths', 'FileField Paths failed to create directory (%d).', array(
        '%d' => $path,
      ), WATCHDOG_ERROR);

      // Restore message.
      $_SESSION['messages'] = $messages;
      return FALSE;
    }
  }

  // Restore message.
  $_SESSION['messages'] = $messages;

  // Move file.
  if (!file_move($source, $file['filepath'])) {
    watchdog('filefield_paths', 'FileField Paths failed to move file (%o) to (%n).', array(
      '%o' => $source,
      '%n' => $file['filepath'],
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // Update database.
  drupal_write_record('files', $file, array(
    'fid',
  ));
  return TRUE;
}