You are here

function upload_save in Drupal 5

Same name and namespace in other branches
  1. 4 modules/upload.module \upload_save()
  2. 6 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 680
File-handling and attaching files to nodes.

Code

function upload_save(&$node) {
  if (!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 ($file->remove) {

      // Remove file previews...
      if (strpos($file->fid, 'upload') !== FALSE) {
        file_delete($file->filepath);
      }
      else {
        db_query('DELETE FROM {file_revisions} WHERE fid = %d AND vid = %d', $fid, $node->vid);

        // Only delete a file if it isn't used by any revision
        $count = db_result(db_query('SELECT COUNT(fid) FROM {file_revisions} WHERE fid = %d', $fid));
        if ($count < 1) {
          db_query('DELETE FROM {files} WHERE fid = %d', $fid);
          file_delete($file->filepath);
        }
      }
    }
    elseif (strpos($file->fid, 'upload') !== FALSE) {
      if ($file = file_save_upload($file, $file->filename)) {
        $file->fid = db_next_id('{files}_fid');
        db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
        db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);

        // Tell other modules where the file was stored.
        $node->files[$fid] = $file;
      }
      unset($_SESSION['file_previews'][$fid]);
    }
    elseif ($node->old_vid && is_numeric($fid)) {
      db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);
    }
    else {
      db_query("UPDATE {file_revisions} SET list = %d, description = '%s' WHERE fid = %d AND vid = %d", $file->list, $file->description, $file->fid, $node->vid);
    }
  }
}