Programmatically create a file node from a saved file


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $file_field = field_file_save_file($temp, $validators = array(), file_create_path($destination), $account = NULL);
  2.  
  3. if(count($file_field) > 0){
  4. // Create a new file node after saving the file
  5. $img_node = new stdClass();
  6. $img_node->title = $file_field['filename'];
  7. $img_node->body = '';
  8. $img_node->status = 1;
  9. $img_node->type = 'image';
  10. $img_node->created = time();
  11. $img_node->changed = time();
  12. $img_node->uid = 1;
  13. $img_node->status = 1;
  14. $img_node->active = 1;
  15. $img_node->promote = 1;
  16. $img_node->field_image = array(
  17. 'fid' => $file_field['fid'],
  18. 'title' => basename($file_field['filename']),
  19. 'filename' => $file_field['filename'],
  20. 'filepath' => $file_field['filepath'],
  21. 'filesize' => $file_field['filesize'],
  22. 'mimetype' => $file_field['filemime'],
  23. 'description' => basename($file_field['filename']),
  24. 'list' => 1,
  25. ),
  26. );
  27.  
  28. node_save($img_node);

URL: http://drupal.org/node/330421

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.