upload_save

modules/upload/upload.module, line 380

Versions
4.6
upload_save($node)
4.7 – 6
upload_save(&$node)
7
upload_save($node)

Code

<?php
function upload_save(&$node) {
  if (empty($node->files) || !is_array($node->files)) {
    return;
  }

  foreach ($node->files as $fid => $file) {
    // Convert file to object for compatibility
    $file = (object)$file;

    // Remove file. Process removals first since no further processing
    // will be required.
    if (!empty($file->remove)) {
      db_query('DELETE FROM {upload} WHERE fid = %d AND vid = %d', $fid, $node->vid);

      // If the file isn't used by any other revisions delete it.
      $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $fid));
      if ($count < 1) {
        file_delete($file->filepath);
        db_query('DELETE FROM {files} WHERE fid = %d', $fid);
      }

      // Remove it from the session in the case of new uploads,
      // that you want to disassociate before node submission.
      unset($node->files[$fid]);
      // Move on, so the removed file won't be added to new revisions.
      continue;
    }

    // Create a new revision, or associate a new file needed.
    if (!empty($node->old_vid) || $file->new) {
      db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES (%d, %d, %d, %d, '%s', %d)", $file->fid, $node->nid, $node->vid, $file->list, $file->description, $file->weight);
      file_set_status($file, FILE_STATUS_PERMANENT);
    }
    // Update existing revision.
    else {
      db_query("UPDATE {upload} SET list = %d, description = '%s', weight = %d WHERE fid = %d AND vid = %d", $file->list, $file->description, $file->weight, $file->fid, $node->vid);
      file_set_status($file, FILE_STATUS_PERMANENT);
    }
  }
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.