Drupal 5: Programmatically create a CCK node that has an imagefield
Recently, I was using a REST implementation to fetch images from another server. Originally, I wrote the code to save the retrieved images as attachments to a single node. I revised the code to create custom separate custom CCK nodes using the imagefield module. Here’s a code snippet that programmatically creates the new nodes:
<?php
$node = new StdClass();
$node->type = 'MYNODETYPE';
$node->title = 'MYNODETITLE';
$file_temp = file_get_contents($tempFile);
$file_temp = file_save_data($file_temp, file_directory_path() .'/' . $fileName, FILE_EXISTS_RENAME);
$node->field_MYIMAGEFIELD = array(
array(
'fid' => 'upload',
'title' => basename($file_temp),
'filename' => basename($file_temp),
'filepath' => $file_temp,
'filesize' => filesize($file_temp),
'filemime' => mime_content_type($file_temp),
),
);
$node->uid = $GLOBALS['user']->uid;
$node->status = 1;
$node->name = $GLOBALS['user']->name;
// save node
node_save($node);
?>