I've been staring at file.inc all day, and I can see no way to change a file name using this API.

Comments

eap1935’s picture

Title: Need way to change filename in file.inc » file_rename() function needed in file.inc

Changed version to cvs - oops.

ax’s picture

file_move(&$source, $dest = 0, $replace = 0)?! reopen if this doesn't do what you want.

Anonymous’s picture

jc.vdb’s picture

Version: » 6.0
Component: base system » file system
Category: feature » support
Status: Closed (fixed) » Postponed (maintainer needs more info)

I don't understand how to rename a file.

The problem in the file_move() funciton is that the $dest parameter must be a folder.
Is there a way to copy a file like the copy function in php?

Here's an example of what i would like to do:

if ( $file[1] = file_save_upload ( 'upload1' , $validators = array ( 'file_validate_image_resolution' => array ('120x120' , '120x120' ) ) ) ){
  file_move($file[1],'uploads/images/'$id);
  session_set('upload_file',$file[1]->filepath);
}else{
  form_set_error('upload1',"File upload 1 failed");
}
drewish’s picture

i think if you specify a full path to file_move it'll honor that... did you actually try it?

drewish’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

oligoelemento’s picture

Solution: change global $_FILES 'name' parameter

$_FILES['files']['name']['upload1']='upload1_new_name';
if ($file = file_save_upload ( 'upload1_new_name' , array(), $directory, FILE_EXISTS_RENAME)){
  drupal_set_message(t('The attached files were successfully uploaded.'));
  file_set_status($file, FILE_STATUS_PERMANENT);
}else{
  drupal_set_message(t('The attached files failed to upload.'));
}