Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (isset($uploads)) {
- foreach ($uploads as $upload) {
- if (isset($upload)) {
- $file = new stdClass;
- $file->uid = 1;
- $file->uri = $upload->filepath;
- $file->filemime = file_get_mimetype($upload->uri);
- $file->status = 1;
- $file = file_copy($file, 'public://images');
- $node->field_image[$node->language][] = (array) $file;
- }
- }
- }
- node_save($node);
- if (isset($uploads)) {
- foreach ($uploads as $upload) {
- $upload->status = 1;
- file_save($upload);
- $node->field_image[$node->language][] = (array) $upload;
- }
- }
- }
- node_save($node);
- <?php
- // Bootstrap Drupal
- define('DRUPAL_ROOT', getcwd());
- require_once './includes/bootstrap.inc';
- require_once './includes/file.inc';
- drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
- // Construct the new node object.
- $path = 'Documents/document1.doc';
- $filetitle = 'test';
- $filename = 'document1.doc';
- $node = new StdClass();
- $file_temp = file_get_contents($path);
- //Saves a file to the specified destination and creates a database entry.
- $file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);
- $node->title = $filetitle;
- $node->body[LANGUAGE_NONE][0]['value'] = "The body of test upload document.nnAdditional Information";
- $node->uid = 1;
- $node->status = 1;
- $node->type = 'document';
- $node->language = 'und';
- $node->field_document_files = array(
- 'und' => array(
- 0 => array(
- 'fid' => $file_temp->fid,
- 'filename' => $file_temp->filename,
- 'filemime' => $file_temp->filemime,
- 'uid' => 1,
- 'uri' => $file_temp->uri,
- 'status' => 1,
- 'display' => 1
- )
- )
- );
- $node->field_taxonomy = array('und' => array(
- 0 => array(
- 'tid' => 76
- )
- ));
- node_save($node);
- ?>
- $path = './sites/default/files/test.jpg';
- $filetitle = 'test';
- $filename = 'test.jpg';
- $node = new StdClass();
- $file_temp = file_get_contents($path);
- $file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);
- $node->title = $filetitle;
- $node->uid = 1;
- $node->status = 1;
- $node->type = '[content_type]';
- $node->language = 'und';
- $node->field_images = array(
- 'und' => array(
- 0 => array(
- 'fid' => $file_temp->fid,
- 'filename' => $file_temp->filename,
- 'filemime' => $file_temp->filemime,
- 'uid' => 1,
- 'uri' => $file_temp->uri,
- 'status' => 1
- )
- )
- );
- $node->field_taxonomy = array('und' => array(
- 0 => array(
- 'tid' => 76
- )
- ));
- node_save($node);
- // Add file upload widget
- // Use the #managed_file FAPI element to upload a document.
- $form['response_document'] = array(
- '#title' => t('Attach a response document'),
- '#type' => 'managed_file',
- '#description' => t('Please use the Choose file button to attach a response document<br><strong>Allowed extensions: pdf doc docx</strong>.'),
- '#upload_validators' => array('file_validate_extensions' => array('pdf doc docx')),
- '#upload_location' => 'public://my_destination/response_documents/',
- );
- $form['node'] = array('#type' => 'value', '#value' => $node);
- $values = $form_state['values'];
- $node = $values['node'];
- // Load the file and save it as a permanent file, attach it to our $node.
- $file = file_load($values['response_document']);
- if ($file) {
- $file->status = FILE_STATUS_PERMANENT;
- file_save($file);
- // Attach the file to the node.
- $wrapper = entity_metadata_wrapper('node', $node);
- $wrapper->field_response_files[] = array(
- 'fid' => $file->fid,
- 'display' => TRUE,
- 'description' => $file->filename,
- );
- node_save($node);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement