You are here

function upload_save in Drupal 6

Same name and namespace in other branches
  1. 4 modules/upload.module \upload_save()
  2. 5 modules/upload/upload.module \upload_save()
1 call to upload_save()
upload_nodeapi in modules/upload/upload.module
Implementation of hook_nodeapi().

File

modules/upload/upload.module, line 389
File-handling and attaching files to nodes.

Code

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);
    }
    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);
    }
  }
}