2

I have an image field that I am trying to save multiple images to. There is one field but when I set up the array, only the first image fid is saved.

$i = 0;
foreach ($photos as $photo) {
// make sure we actually retrived this particular image
if ($photo['Success'] == TRUE) {
    // setup the filename and filepath, then write the file to disk
    $filename = "{$id}-{$number}.jpg";
    $filepath = "public://drealty_img/{$filename}";

    // Create image object
    $file = file_save_data($photo['Data'], $filepath, FILE_EXISTS_REPLACE);

    // add this image to the content entity
    $content->images = array(
        'und' => array(
            $i => array(
                'fid' => $file->fid,
            ),
        ),
    );
} // endif $photo['Sucess']
$i++;
} // endfor $photos

Am I saving the image field incorrectly? Or is there something that needs to be set up to allow for multiple images to be saved?

1 Answer 1

1

I changed the following code:

 $content->images = array(
        'und' => array(
            $i => array(
                'fid' => $file->fid,
            ),
        ),
    );

to

$content->images['und'][$i] = array(
  'fid' => $file->fid,
);

And it worked

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.