Hi All,
The following code works for Programmatically creating nodes with ImageFields in Drupal 5 but i need to save for Drupal 6. In Drupal 6 , how can I get it working as the CCk image filed is now filefield. Can anyone help to get it working in Drupal 6?

  $node = new StdClass();
  $node->type = 'official_photo';
  $node->title = t('Sample official photo');
  $node->taxonomy[4] = 15; // Candid shots gallery
  $file_temp = file_get_contents('./profiles/multisite/files/official-photo.png');
  $file_temp = file_save_data($file_temp, file_directory_path() .'/official_photos/official-photo.png', FILE_EXISTS_RENAME);
  $node->field_photo = array(
    array(
      'fid' => 'upload',
      'title' => basename($file_temp),
      'filename' => basename($file_temp),
      'filepath' => $file_temp,
      'filesize' => filesize($file_temp),
    ),
  );
  $node->uid = 1;
  $node->status = 1;
  $node->active = 1;
  $node->promote = 1;
  node_save($node);

Thanks in advance.
//dewparadise

Comments

danielb’s picture

Did you first export/import the content type, and ensure the imagefield module was enabled?

dewparadise’s picture

I did not get your point? what do you mean by that? Any reference or snippet??

nathanjo’s picture

For some reason I make it working in this simple way:

<?php
      $filepath = file_directory_path() . '/images/'. $image;
      if (file_exists($filepath)) {
        $field = field_file_save_file($filepath, array(), file_directory_path());
        $node->field_photo = array(0 => $field);
      }
?>
SweedyMick’s picture

Thank you a lot, after having tried a lot of possibilities with all sorts of parameters which all failed, your solution is simple and just works!

macramole’s picture

thanks, working like a charm.

I'm showing an upload form from outside drupal and then creating the node programatically. The only thing is that I have to copy from the PHP temporary location to somewhere else and then use the field_file_save_file function to save it in the final place.. after that delete the file at "somewhere else".. this is because if you pass $_POST['file']['tmp_name'] as filepath, then you'll have a .tmp in your files directory instead of a .jpg or whatever.. not a big problem but still..

thanks again mate

xiong’s picture

Great snippet!

schiavone’s picture

Here's a slight change to handle multiple images/files.

<?php
     //YOUR LOOP HERE
      $filepath = file_directory_path() . '/images/'. $image;
      if (file_exists($filepath)) {
        $field = field_file_save_file($filepath, array(), file_directory_path());
        $node->field_photo[] = $field);
      }
?>
steveOR’s picture

WORKS IN SUCH A FASHION AS TO MAKE ME SMILE :) Yes its nearly 2013 and I am still coding Drupal 6 like there is no tomorrow.

spiderplant0’s picture

If anyone else got confused about where to place nathanjo's mod for the image field and where the image 'title' and 'alt' have got to, here is the full code..

  $node = new StdClass();
  $node->type = 'official_photo';
  $node->title = t('Sample official photo');
  $node->taxonomy[4] = 15; // Candid shots gallery

  $node->uid = 1;
  $node->status = 1;
  $node->active = 1;
  $node->promote = 1;

  $field = field_file_save_file(
    ./profiles/multisite/files/official-photo.png', 
    array(), 
    'new/image/path');

  if( !isset($field['data']) ) $field['data'] = array();
  $field['data']['title'] = "the title";
  $field['data']['alt'] = "the alt";

  $node->field_theme_preview_thumb = array(0 => $field);

  $node = node_submit($node);
  node_save($node);
  //var_dump($node);