You are here

function filefield_paths_file_move in File (Field) Paths 5

Same name and namespace in other branches
  1. 6 filefield_paths.module \filefield_paths_file_move()

Move file and update its database record.

1 call to filefield_paths_file_move()
filefield_paths_filefield_paths_process_file in ./filefield_paths.module
Implementation of hook_filefield_paths_process_file().

File

./filefield_paths.module, line 602
Adds extra functionality to FileFields Path settings.

Code

function filefield_paths_file_move(&$file, $replace = FILE_EXISTS_RENAME) {
  $dest = _filefield_paths_strip_path(dirname($file['filepath']['new']));
  foreach (explode('/', $dest) as $dir) {
    $dirs[] = $dir;
    $path = file_create_path(implode($dirs, '/'));
    if (!_filefield_paths_check_directory($path, FILE_CREATE_DIRECTORY)) {
      watchdog('filefield_paths', t('FileField Paths failed to create directory (%d).', array(
        '%d' => $path,
      )), WATCHDOG_ERROR);
      return FALSE;
    }
  }
  if (!file_move($file['field']['filepath'], $dest . '/' . $file['filename']['new'], $replace)) {
    watchdog('filefield_paths', t('FileField Paths failed to move file (%o) to (%n).', array(
      '%o' => $file['filepath']['old'],
      '%n' => $dest . '/' . $file['filename']['new'],
    )), WATCHDOG_ERROR);
    return FALSE;
  }
  $result = db_fetch_object(db_query("SELECT origname FROM {files} WHERE fid = %d", $file['field']['fid']));

  // Set 'origname' and update 'filename'.
  if (empty($result->origname)) {
    db_query("UPDATE {files} SET filename = '%s', filepath = '%s', origname = '%s' WHERE fid = %d", $file['filename']['new'], $file['field']['filepath'], $file['filename']['old'], $file['field']['fid']);
  }
  else {
    db_query("UPDATE {files} SET filename = '%s', filepath = '%s' WHERE fid = %d", $file['filename']['new'], $file['field']['filepath'], $file['field']['fid']);
  }
  return TRUE;
}