Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. if (isset($uploads)) {
  2. foreach ($uploads as $upload) {
  3. if (isset($upload)) {
  4. $file = new stdClass;
  5. $file->uid = 1;
  6. $file->uri = $upload->filepath;
  7. $file->filemime = file_get_mimetype($upload->uri);
  8. $file->status = 1;
  9.  
  10. $file = file_copy($file, 'public://images');
  11.  
  12. $node->field_image[$node->language][] = (array) $file;
  13. }
  14. }
  15. }
  16.  
  17. node_save($node);
  18.  
  19. if (isset($uploads)) {
  20. foreach ($uploads as $upload) {
  21. $upload->status = 1;
  22.  
  23. file_save($upload);
  24.  
  25. $node->field_image[$node->language][] = (array) $upload;
  26. }
  27. }
  28. }
  29.  
  30. node_save($node);
  31.  
  32. <?php
  33. // Bootstrap Drupal
  34. define('DRUPAL_ROOT', getcwd());
  35. require_once './includes/bootstrap.inc';
  36. require_once './includes/file.inc';
  37. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  38.  
  39. // Construct the new node object.
  40. $path = 'Documents/document1.doc';
  41. $filetitle = 'test';
  42. $filename = 'document1.doc';
  43.  
  44. $node = new StdClass();
  45.  
  46. $file_temp = file_get_contents($path);
  47.  
  48. //Saves a file to the specified destination and creates a database entry.
  49. $file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);
  50.  
  51. $node->title = $filetitle;
  52. $node->body[LANGUAGE_NONE][0]['value'] = "The body of test upload document.nnAdditional Information";
  53. $node->uid = 1;
  54. $node->status = 1;
  55. $node->type = 'document';
  56. $node->language = 'und';
  57. $node->field_document_files = array(
  58. 'und' => array(
  59. 0 => array(
  60. 'fid' => $file_temp->fid,
  61. 'filename' => $file_temp->filename,
  62. 'filemime' => $file_temp->filemime,
  63. 'uid' => 1,
  64. 'uri' => $file_temp->uri,
  65. 'status' => 1,
  66. 'display' => 1
  67. )
  68. )
  69. );
  70. $node->field_taxonomy = array('und' => array(
  71. 0 => array(
  72. 'tid' => 76
  73. )
  74. ));
  75. node_save($node);
  76. ?>
  77.  
  78. $path = './sites/default/files/test.jpg';
  79. $filetitle = 'test';
  80. $filename = 'test.jpg';
  81.  
  82. $node = new StdClass();
  83.  
  84. $file_temp = file_get_contents($path);
  85. $file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);
  86.  
  87. $node->title = $filetitle;
  88. $node->uid = 1;
  89. $node->status = 1;
  90. $node->type = '[content_type]';
  91. $node->language = 'und';
  92. $node->field_images = array(
  93. 'und' => array(
  94. 0 => array(
  95. 'fid' => $file_temp->fid,
  96. 'filename' => $file_temp->filename,
  97. 'filemime' => $file_temp->filemime,
  98. 'uid' => 1,
  99. 'uri' => $file_temp->uri,
  100. 'status' => 1
  101. )
  102. )
  103. );
  104. $node->field_taxonomy = array('und' => array(
  105. 0 => array(
  106. 'tid' => 76
  107. )
  108. ));
  109. node_save($node);
  110.  
  111. // Add file upload widget
  112. // Use the #managed_file FAPI element to upload a document.
  113. $form['response_document'] = array(
  114. '#title' => t('Attach a response document'),
  115. '#type' => 'managed_file',
  116. '#description' => t('Please use the Choose file button to attach a response document<br><strong>Allowed extensions: pdf doc docx</strong>.'),
  117. '#upload_validators' => array('file_validate_extensions' => array('pdf doc docx')),
  118. '#upload_location' => 'public://my_destination/response_documents/',
  119. );
  120.  
  121. $form['node'] = array('#type' => 'value', '#value' => $node);
  122.  
  123. $values = $form_state['values'];
  124. $node = $values['node'];
  125. // Load the file and save it as a permanent file, attach it to our $node.
  126. $file = file_load($values['response_document']);
  127. if ($file) {
  128. $file->status = FILE_STATUS_PERMANENT;
  129. file_save($file);
  130.  
  131. // Attach the file to the node.
  132. $wrapper = entity_metadata_wrapper('node', $node);
  133. $wrapper->field_response_files[] = array(
  134. 'fid' => $file->fid,
  135. 'display' => TRUE,
  136. 'description' => $file->filename,
  137. );
  138. node_save($node);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement