Import files into a file field in Drupal 7 I have a content type that accepts a PDF document as a file attachment.  I have a bunch of entries that currently have no attachment, but I have the files for each entry and need to associate the files with each of their respective entries.  

Currently, all of those files are stored in public://publications, so they're already in the public file path where they should be, I just need to "hook them up".

Rather than hack this with Ruby which I would normally do, I'd like to kick the bad habit and use the Drupal API.  I've looked at the following articles:


Programmatically attaching files
http://drupal.org/node/330421


..but haven't been able to really wrap my head around it.

Curious if others have dealt with this situation and what they've done to address it.  Thanks.

UPDATE

I've got the files copying as they should be.  However, I'm not encountering an error, and the error happens during the node_save call.

Here is the error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array
(
    [:db_insert_placeholder_0] =&gt; 0
    [:db_insert_placeholder_1] =&gt; 1
    [:db_insert_placeholder_2] =&gt; 1330491978
)
 in drupal_write_record() (line 6975 of /var/www/website/drupal-7.4/includes/common.inc).


And here is the code.

$node = node_load($bib_value-&gt;nid);

$file = new stdClass();
$file-&gt;filename = basename($filepath);
$file-&gt;filepath = $filepath;
$file-&gt;filemime = file_get_mimetype($filepath);
$file-&gt;filesize = filesize($filepath);
$file-&gt;uid = 1;
$file-&gt;status = FILE_STATUS_PERMANENT;
$file-&gt;uri = $filepath;
$file = file_copy($file,'public://publications',FILE_EXISTS_REPLACE);

// THIS IS A PROBLEM LINE
$node-&gt;field_publication_attachment[LANGUAGE_NONE] = $file;

node_save($node);


If I comment out the line labeled THIS IS A PROBLEM LINE then no error occurs, but that obviously isn't desirable because that means it's completing but without attaching my file to the node!  Help!

SOLUTION

$node = node_load($bib_value-&gt;nid);

$file = new stdClass();
$file-&gt;filename = basename($filepath);
$file-&gt;filepath = $filepath;
$file-&gt;filemime = file_get_mimetype($filepath);
$file-&gt;filesize = filesize($filepath);
$file-&gt;uid = 1;
$file-&gt;status = FILE_STATUS_PERMANENT;
$file-&gt;uri = $filepath;
$file-&gt;display = 1;
$file-&gt;description = '';
$file = file_copy($file,'public://publications',FILE_EXISTS_REPLACE);

$node-&gt;field_publication_attachment[LANGUAGE_NONE][] = (array)$file;

node_save($node);


Needed to set display and description on $file as well as cast $file as an array when adding it to the attachment field.  All worked smoothly after that.
I am currently working on the exact same thing, except using photos.  If you click my profile name, you will see a series of questions related to learning how to do what you are asking to do.

I was able to easily accomplish the code to loop through all the files in my folder and add them to my nodes. 

$file = new stdClass();
$file-&gt;filename = basename($local_path);
$file-&gt;filepath = $local_path;
$file-&gt;uri = $local_path;
$file-&gt;filemime = $mime;
$file-&gt;filesize = filesize($local_path);

// Set this at the very beginning to correspond to USER you want posting items.
$file-&gt;uid = $uid; 
$file-&gt;status = FILE_STATUS_PERMANENT;
file_save($file);

$images[] = array(
'fid' =&gt; $file-&gt;fid,
'alt' =&gt; $page_data['title'],
'title' =&gt; $page_data['title']
);