0

I am trying to attach multiple pictures to the node programmatically in drupal 7 but have no idea how. i am not even sure its possible. Here is my code for attaching one picture to the node and it works. Please if someone can help me how to attach multiple pictures more than one to the image field

$node->field_image['und'][0]['fid'] = $get_fid->fid;

Charles
51.5k13 gold badges107 silver badges146 bronze badges
asked Aug 27, 2010 at 21:06

2 Answers 2

2

i got it...first you have to change the image field settings to upload between 1 to 10 or unlimited than its very easy

foreach($sav_fid as $sn_fid) {
 $node->field_image['und'][$cnt_dlt]['fid'] = $sn_fid;
 $cnt_dlt++;
}

this will do it where field_image is the core image field attached to the node api.

['und'] is just a code for language i guess means undefined.

[$cnt_dlt] is a counter variable which initialise 0 than increase by 1 and this part is delta(this number will increase attach the imagae).

[fid] is the field_image reference to the file in the managed_file table.

$sn_fid is the fid(file id) from the managed_file table.

in order to create and attaching picture to a node by using node_save($node); you have upload the file by using file_copy($files); and save the fid by creating an object and saving the result from it.

avpaderno
30k17 gold badges80 silver badges95 bronze badges
answered Aug 28, 2010 at 1:38
Sign up to request clarification or add additional context in comments.

Comments

0

There are benefits if you add file usage counter. A less minimalist approach:

foreach ($sav_fid as $sn_fid) {
 $file = file_load($sn_fid);
 if (isset($file->filename)) {
 file_usage_add($file, 'file', 'node', $node->nid); 
 $node->field_images[LANGUAGE_NONE][] = array(
 'fid' => $file->fid,
 'filename' => $file->filename,
 'filemime' => $file->filemime,
 'uid' => 1,
 'uri' => $file->uri,
 'status' => 1
 );
 }
}
node_save($node);
answered Nov 27, 2017 at 16:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.