If you specify an optional file path in a CCK filefield and this directory does not exist inside the Drupal files directory then the first file is stored in the wrong directory. It gets stored in the Drupal files directory and not in the fresh created specified file path dirctory. The reason for this are the following two lines of code inside the function filefield_file_insert() in filefield.module:
$filepath = file_create_path($widget_file_path) . '/' . $file['filename'];
if (filefield_check_directory($widget_file_path) && $file = file_save_upload((object)$file, $filepath)) {
In the first line if the optional file path in $widget_file_path does not exist file_create_path() returns FALSE. In the second line after filefield_check_directory() has created the missing directoy or directories in $widget_file_path the following function file_save_upload() stores the file in the Drupal files directory because $ filepath does not include the destination directory in $widget_file_path.
I fixed this bug with following code (keeping the 2 originals code lines commentet out):
// $filepath = file_create_path($widget_file_path) . '/' . $file['filename'];
$filepath = file_create_path($widget_file_path);
if ($filepath) {
$filepath = $filepath . '/' . $file['filename'];
} else {
if (filefield_check_directory($widget_file_path)) {
$filepath = file_create_path($widget_file_path);
if ($filepath) {
$filepath = $filepath . '/' . $file['filename'];
}
}
}
// if (filefield_check_directory($widget_file_path) && $file = file_save_upload((object)$file, $filepath)) {
if ($filepath && $file = file_save_upload((object)$file, $filepath)) {
As an addition I respect the return value of filefield_check_directory() in the code above. If filefield_check_directory() returns FALSE the file objet is not written into the table "files" To achieve this I also had to modify the function filefield_check_directory(). Now this function returns FALSE if the call to Drupal function file_check_directory() returns FALSE. Here is my modification, again I keep the original line I replaced commentet out:
// file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
if (FALSE == file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0])) {
return FALSE;
}
There seems to be another non critical error here: if $form_element['#parents'][0] is undefined, as it is here because filefield_check_directory() is called without the 2nd optional parameter $form_element, you get an non critical error. I did not care for this error this time.
I attach a zipped patch file with my modifications. If someone wants to apply this patch please unzip the file inside the filefield module folder and apply the patch with the command "patch -p0 < filefield.module-patch.
Comments
Comment #1
brahms commentedPlease forget my attempt to fix the bug above. It is the wrong solution. There is a much better patch for it in comment#2 of issue 275293.