I have a very straight forward content type created in Drupal - Title, Body, Taxonomy term (drop down select, 1 value only), and Photos (multiple permitted, up to 8, alt and title tags are enabled). I also have the meta
I have a set of CSV files with data for those fields with corresponding photos. I need to iterate over these programatically and use node_save() to get these in the database as well as upload the related photos.
I followed the direction here, and while it was a good start, I am still confused as to how to handle multiple uploads.
Bulk saving nodes with a file field
Can any suggest a tutorial or provide some sample code that can better illustrate how to do this?
EDIT: Based on the direction below, this is the code I have come up with
$images = array();
foreach ($file_data_array as $csv_data) {
foreach($image_array as $file_data) {
$file = new stdClass();
$file->filename = basename($file_data['filepath']);
$file->filepath = $file_data['filepath'];
$file->filemime = $file_data['mime'];
$file->filesize = filesize($file_data['filepath']);
$file->uid = $uid;
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$images[] = array(
'fid' => $file->fid,
'alt' => $file_data['alt'],
'title' => $file_data['title']
);
}
$node = new stdClass;
$node->type = 'baby_clothes';
$node->body = 'body';
$node->title = 'title';
$node->uid = 1;
$node->status = 1;
$node->active = 1;
//the name of my actual field is 'product images'
$node->field_product_images[LANGUAGE_NONE] = $images;
//this is my actual taxonomy field
$node->field_baby_clothes_category[LANGUAGE_NONE][0]['tid'] = 2; //actual ID of the taxonomy for this node
node_save($node);
}
Thoughts?