Is there a simple way to attach a file that is already on the server to a node from php? Just using the standard core upload module.

Something like file_attach($nid,$filenamewithpath)?

Anton

Comments

bwv’s picture

subscribing
----------------------------------------------------------------------
http://www.bwv810.com/

I am a writer and researcher. In my spare time I build websites with Drupal.
Je peux communiquer en français. / Я могу общаться на русском языке.

asciikewl’s picture

I eventually did this with the following code:

function xxx_attach_file_to_node($nid,$filename,$mimetype,$description) {
  if (!is_file($filename)) {
    drupal_set_message("File $filename not found");
    return;
  }
  $destpath = file_directory_path()."/file2node/$nid";
  if (!file_exists($destpath)) {
    mkdir($destpath,0770,TRUE);
  }
  $basename = trim(basename($filename), '.');
  $size = filesize($filename);
  $fid = db_next_id('{files}_fid');
  if (dirname($filename) != $destpath) {
    file_copy($filename,$destpath);
  }
  $vid = db_result(db_query("SELECT vid from {node} where nid=%d",$nid));

  db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) ".
    "VALUES (%d, %d, '%s', '%s', '%s', %d)",
    $fid, $nid, $basename, $filename, $mimetype, $size);
  db_query("INSERT INTO {file_revisions} (fid, vid, list, description) " .
    "VALUES (%d, %d, %d, '%s')", $fid, $vid, 1, $description);
}
bwv’s picture

Can you please explain how you used this code? Did you paste it into the upload module? This is a feature I've been trying to finesse for a long time. Many thanks.

----------------------------------------------------------------------
http://www.bwv810.com/

I am a writer and researcher. In my spare time I build websites with Drupal.
Je peux communiquer en français. / Я могу общаться на русском языке.