I am not the PHP / Drupal guru but when I looked into FileNode code I have a feeling there is an error in function _filenode_insert_file_with_realpath concept.

The idea is that if the file does not exist in the file system it is uploaded and the new entry is created in table file.
Then "file" record is returned back to create an entry in filenode tableusing fid (FileID) from "file" record.

The problem is that Drupal's function db_last_insert_id on MySQL returns not the "next" free ID to be used but the last inserted ID.
filenode.imce.inc lines 111-118:

      $fid = db_last_insert_id("file", "id");
      $filename = basename($file_realpath);
      $filemime = file_get_mimetype($filename);
      $filesize = filesize($file_realpath);
      db_query("INSERT INTO {files} 
      (fid, uid, filename, filepath, filemime, filesize, status, timestamp)
      VALUES (%d, %d, '%s', '%s', '%s', %d, %d, UNIX_TIMESTAMP())", 
      $fid, $user->uid, $filename, $filepath, $filemime, $filesize, 1); // use uid=1 for all of these uploads

As long as table files contains fid defined as AUTO_INCREMENT the fid in the insert should be simply passed by.
filenode.imce.inc lines 111-118 (fixed):

      // $fid = db_last_insert_id("file", "id");
      $filename = basename($file_realpath);
      $filemime = file_get_mimetype($filename);
      $filesize = filesize($file_realpath);
      db_query("INSERT INTO {files} 
      (uid, filename, filepath, filemime, filesize, status, timestamp)
      VALUES (%d, '%s', '%s', '%s', %d, %d, UNIX_TIMESTAMP())", 
      $user->uid, $filename, $filepath, $filemime, $filesize, 1); // use uid=1 for all of these uploads

If you do not patch the code the risk is that you will create a filenode linked to "nothing" as SQL insert statement will return the error due to primary key constraint violation.

Comments

wilkinson_james’s picture

This fixed our empty file node file path issue we where having with our Drupal 6.20 installation.