When I upload file which size more than 500Mb and try too attach this file to node I have a problem:
Because this module using file_unmanaged_copy my file not MOVED! It's copied to new location. So this request more time that allow script for executing.
Is there are posibility to use rename() function for moving files instead copy() and unlink() after it?

Comments

quicksketch’s picture

This would be a great improvement. I'm sure it's possible in some way but the FileAPI makes using file move less than obvious.

quicksketch’s picture

Title: Problem with attaching BIG files » Problem with attaching BIG files (use move instead of copy/delete)
jwaxman’s picture

This would be very helpful.
My most important reason for using this module is for large video files.
Using move/rename would save substantial server resources and time.
Is there another way to approach this?

robin_b’s picture

Version: 7.x-1.6 » 6.x-1.7
StatusFileSize
new7.65 KB

I found a solution for this, however it involved adding a function to the core. I saw no other way because it's not possible to move a file in Drupal. Everything is done with copy/delete even the core function file_move().

I use this module especially for big video files (+600MB) and half the times it crashes because it takes too long to copy the file.

I basically copied the core function file_copy() (file.inc) and named it file_real_move().
I edited this line

if (!@copy($source, $dest)) {

to:

if (!@rename($source, $dest)) {

Next, I edited the module filefield, file field_file.inc, function field_file_save_file() and added an extra function parameter named $copy_file

function field_file_save_file($filepath, $validators = array(), $dest = FALSE, $account = NULL, $copy_file = TRUE) {

I also changed this part in the function:

  if($copy_file){ //copy the file
    if (!file_copy($file, $file->destination, FILE_EXISTS_RENAME)) {
      form_set_error($file->source, t('File upload error. Could not move uploaded file.'));
      watchdog('file', 'Upload error. Could not move file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->destination));
      return 0;
    }
  }else{ // move the file instead of copying
    if(!<strong>file_real_move</strong>($file, $file->destination, FILE_EXISTS_RENAME)){
      form_set_error($file->source, t('File upload error. Could not move uploaded file.'));
      watchdog('file', 'Upload error. Could not move file %file to destination %destination. Tried to move instead of copying', array('%file' => $file->filename, '%destination' => $file->destination));
      return 0;
    }
  }

The default value of $copy_file is TRUE, so everything that calls this function will keep functioning as before.

The last thing I did was altering file attach.inc, module filefield_sources, function filefield_source_attach_value(). I added the parameter FALSE to field_file_save_file, so the above function moves the file instead of copying it.

    // Save the file to the new location. The file will be really moved (not copied/deleted).
    if ($file = field_file_save_file($filepath, $validators, $directory, NULL, FALSE)) {
      $item = array_merge($item, $file);
    }

I provided a Drupal6 patch for this not-done way. If you apply this change, be sure to document it, so you remember it when you update the core or this module.

quicksketch’s picture

Status: Active » Patch (to be ported)

Unfortunately I'm no longer updating the Drupal 6 version of the module. This change will need to be ported to D7. It's a good approach and patch, but for me it's not worth the effort of maintaining the D6 module any more.

quicksketch’s picture

Actually I take that back. It looks like D6 is still getting maintenance updates but no new features. As a bug, this seems like it could/should apply to both versions of the module. Unfortunately file handling changed quite a bit between D6/D7, so porting requires some work.

kenorb’s picture

Version: 6.x-1.7 » 7.x-1.6
Issue summary: View changes
Status: Patch (to be ported) » Fixed
kenorb’s picture

Version: 7.x-1.6 » 7.x-1.x-dev
Status: Fixed » Needs work