file_move

5 file.inc file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME)
6 file.inc file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME)
7 file.inc file_move(stdClass $source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8 file.inc file_move(stdClass $source, $destination = NULL, $replace = FILE_EXISTS_RENAME)

Moves a file to a new location.

  • Checks if $source and $dest are valid and readable/writable.
  • Performs a file move if $source is not equal to $dest.
  • If file already exists in $dest either the call will error out, replace the file or rename the file based on the $replace parameter.

Parameters

$source: Either a string specifying the file location of the original file or an object containing a 'filepath' property. This parameter is passed by reference and will contain the resulting destination filename in case of success.

$dest: A string containing the directory $source should be copied to. If this value is omitted, Drupal's 'files' directory will be used.

$replace: Replace behavior when the destination file already exists.

Return value

TRUE for success, FALSE for failure.

Related topics

1 call to file_move()

File

includes/file.inc, line 355
API for handling file uploads and server file management.

Code

function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
  $path_original = is_object($source) ? $source->filepath : $source;

  if (file_copy($source, $dest, $replace)) {
    $path_current = is_object($source) ? $source->filepath : $source;

    if ($path_original == $path_current || file_delete($path_original)) {
      return 1;
    }
    drupal_set_message(t('The removal of the original file %file has failed.', array('%file' => $path_original)), 'error');
  }
  return 0;
}

Comments

clarification: $dest can be a full path + new filename

The above documentation may appear to suggest the filename in $source will always become the filename of the $dest file. Also, it implies $dest must be a folder/directory, rather than a path+filename . In D6.14 I've used file_move() as follows:

file_move("/uploads/abc.txt", "/uploads/def.txt"); // rename, keep in same dir

file_move("/uploads/mno.txt", "/uploads/converted/xyz.txt"); // move and rename

This is like unix mv command, and maybe windows, but hopefully my comment will save someone from having to test it all to see if it works for renaming. I've only tried this with absolute paths.

Relative pathes works just fine

I tested file_move() with relative paths and it worked just fine.

There is a little problem, though. If the folder that you are trying to transfer the file to doesn't exist yet, you will get an error message.

It would be great if there was an option on this function to automatically create new folders if they didn't exist.

There is a better way

Here is how the "file_move()" function should have been written in the first place.

It does more or less what the new "file_move()" function for Drupal 7 does.

You can call this function as follows:

move_file(555, 'sites/default/files/image1.jpg', 'sites/default/files/JPG/image1.jpg');

Where '555' is the fid, the first path is the original file path, and the second path is the destination path.

The function is pretty well commented.

<?php
/*
This function will:
1) Create a folder if not existent
2) Move the file into the new location
3) Update the file record on the database

$file_id => Is the fid that you want to move.
$org_path => Is the full original file path including directory tree and file name.
$new_path => Is the full destination file path including directory tree and file name.
*/
function move_file($file_id, $org_path, $new_path){
 
 
//Get just the directory path from the full file path.
 
$new_dir_path = substr($new_path, 0, strrpos($new_path, '/'));
 
 
//If new directory path does not exist, create it.
 
file_check_directory($new_dir_path, $mode = FILE_CREATE_DIRECTORY);
 
 
//Try to move the file into the new location.
 
$success = file_move($org_path, $new_path, $replace = FILE_EXISTS_REPLACE);
 
 
//If the file move was successful, update the database record.
 
if ($success){
 
   
$result = db_query('UPDATE {files} SET filepath = "%s" WHERE fid = %d', $new_path, $file_id);
  }
}
?>

I checked the schema of files

I checked the schema of files table and found that fields required to update not only filepath field, but filename field.

Login or register to post comments