Users may specify a custom directory for file attachments on file fields. However, if the directory is not already created on the server, the file attachment will fail. This is because line 916 of file.inc passes the destination directory specified to file_prepare_directory(), but does not utilize the FILE_CREATE_DIRECTORY argument. This causes the function to return false when the server does not have the directory already in place.
Because we offer tokens which are dynamic (such as NID) to be used in the file field directory path setting, we need to create paths.
My proposed fix is simple:
(current implementation)
if (!file_prepare_directory($dirname)) {
// The destination is not valid.
watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $original_source, '%destination' => $dirname));
drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $original_source)), 'error');
return FALSE;
}
}
(fixes the bug)
if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
// The destination is not valid.
watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $original_source, '%destination' => $dirname));
drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $original_source)), 'error');
return FALSE;
}
}
Comments
Comment #1
sheldonkreger commentedHere is the patch.
Comment #2
sheldonkreger commentedComment #3
shivanshuag commentedI am not able to reproduce this issue. Steps I followed -
1. Create a file field in a content type with a custom sub directory, which does not exist, to save the files.
2. Create some content of the content type and upload a file.
I got no error. It created the sub directory to store the file.
I then manually deleted the sub directory and then again created some content and added a file. Still no error.
Comment #4
sheldonkreger commentedThis may be an issue coming from one of the many file-related modules we are using, or server config. I'll mark this as unable to reproduce until I can set up a clean instance.