I've just speed up attaching file from server disk

Created on 21 January 2015, over 10 years ago
Updated 27 June 2025, 10 days ago

It's a great module, but the default behavior is a bit strange. It's very slowly when I moving file from the attaching directory to the target directory.

I looked into the source codes and drupal apis, it used file_unmanaged_copy even if I just need to move file. but file_unmanaged_move does not solve this problem because it's use file_unmanaged_copy inside too ( unbelievable but yes ).

So I did some home work like below, use PHP original function rename instead of file_unmanaged_copy to move files.

I changed sources/attach.inc on line 250, sorry I don't know how to make .diff files and I'm not sure will this is going to crash on other's sites.

// Save the file to the new location.
// Delete the original file if "moving" the file instead of copying.
if ($instance['widget']['settings']['filefield_sources']['source_attach']['attach_mode'] !== 'copy') {
	global $user;
	$file = new stdClass();
	$file->uid      = $user->uid;
	$file->status   = 0;
	$file->filename = trim(basename($filepath), '.');
	$file->uri      = $directory.'/'.$file->filename;
	$file->filemime = file_get_mimetype($file->filename);
	$file->filesize = filesize($filepath);
	watchdog('file', 'move file from %source to %destination', array('%source'=>$filepath, '%destination'=>$file->uri));
	if(@rename($filepath, $file->uri)){
		$item = array_merge($item, (array) file_save($file));
		@unlink($filepath);
	}			
}else{
	if ($file = filefield_sources_save_file($filepath, $validators, $directory)) {
		$item = array_merge($item, (array) $file);
	}
}

It boosts your moving file as a rocket! I used to spend tens of minutes to move large video file, now it only spends seconds, That's great.

Hope this will help any others.

If this is not what you want, never mind, it's a small hack.

If it's not a perfect solution, help me out, Thank you.

📌 Task
Status

Closed: outdated

Version

1.9

Component

Source: Attach (FTP)

Created by

🇨🇳China im0000

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024