Programmatically creating filefield entries

Last modified: October 22, 2009 - 03:36

I've had to bump my head into batch uploading files to a drupal installation. Thanks to the prosepoint developers for the fileupload code in their profile. It's a code for importing single file. Just make a simple loop to import bunch of files.

<?php
  $mime
= 'audio/mpeg'; // I was importing mp3 files

 
$file_drupal_path "path/under/files/directory/file.mp3";

 
$file = new stdClass();
 
$file->filename = basename($file_drupal_path);
 
$file->filepath = $file_drupal_path;
 
$file->filemime = $mime;
 
$file->filesize = filesize($file_drupal_path);

 
$file->uid = $uid;
 
$file->status = FILE_STATUS_PERMANENT;
 
$file->timestamp = time();
 
drupal_write_record('files', $file);
 
$file->fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath));


 
$node = new StdClass();
 
$node->type = 'word';
 
$node->body = $body;
 
$node->title = $word;
 
$node->field_pronounciation = array(
    array(
     
'fid' => $file->fid,
     
'title' => basename($file->filename),
     
'filename' => $file->filename,
     
'filepath' => $file->filepath,
     
'filesize' => $file->filesize,
     
'mimetype' => $mime,
     
'description' => basename($file->filename),
     
'list' => 1,
    ),
  );
 
$node->uid = 1;
 
$node->status = 1;
 
$node->active = 1;
 
$node->promote = 1;
 
node_save($node);
?>

A more elaborate way of importing large amounts of files using batch API can be found in #292904: Mass import/upload?.

This is exactly what I was

upupax - January 12, 2009 - 22:23

This is exactly what I was looking for! Worked perfectly!
I need it to import an image folder and fill each node with an image!
Just in case: using imagefield you could even set image alt and image title in this way

$node->field_img_tel = array(
    array(
'fid' => $file->fid,
'title' => basename($file->filename),
'filename' => $file->filename,
'filepath' => $file->filepath,
'filesize' => $file->filesize,
'mimetype' => $mime,
'description' => basename($file->filename),
'list' => 1,
'data'=>array('alt' => 'image alt',
'title' => 'image title',
),
),
);

Thanks a lot!

Nice to see someone using the code ;)

pkej - January 13, 2009 - 12:56

I even came across this #201983: How To: Migrate from Image.module to ImageField Documentation Project for anyone needing to migrate from image to image field based nodes (as I am needing just this second).

Paul K Egell-Johnsen

Thankyou! finally for drupal 6

divinevette - July 14, 2009 - 23:23

I have been looking around for 2 days for a way to do this! I was missing the "drupal_write_record" though I had the rest correct. Like the original, this code should work for any filefield. I used it for imagefield.

I modified the above to work with drupal_execute rather than node_save because I have read it is the better practice.

Prior to this you need to have your file on the server. Note the variables at the top need to be changed.

Here is the code that is working for me in Drupal 6:

<?php
  $mime
'image/jpeg';
 
$your_node_type = 'custom';
 
$file_drupal_path "sites/default/files/YOUR_FILE.jpeg";

 
$file = new stdClass();
 
$file->filename = basename($file_drupal_path);
 
$file->filepath = $file_drupal_path;
 
$file->filemime = $mime;
 
$file->filesize = filesize($file_drupal_path);

 
$file->uid = $uid;
 
$file->status = FILE_STATUS_PERMANENT;
 
$file->timestamp = time();
 
drupal_write_record('files', $file);
 
$file->fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath));

// Create a new node
$form_state = array(); module_load_include('inc', 'node', 'node.pages');
$node = array('type' => $your_node_type);
$form_state['values']['type'] = $your_node_type; // the node's type
$form_state['values']['title'] = 'Your node title';
$form_state['values']['body'] = 'This is the body text!';

// add CCK fields
$form_state['values']['field_price'][0]['value'] = 14;
$form_state['values']['field_rid'][0]['value'] =13;

//more node values
$form_state['values']['name'] = 'yourusername';
$form_state['values']['op'] = t('Save');

//add image field
$form_state['values']['field_pic'] = array(
    array(
     
'fid' => $file->fid,
     
'title' => basename($file->filename),
     
'filename' => $file->filename,
     
'filepath' => $file->filepath,
     
'filesize' => $file->filesize,
     
'mimetype' => $mime,
     
'description' => basename($file->filename),
     
'list' => 1,
    ),
  );

$errs= drupal_execute($your_node_type.'_node_form', $form_state, (object)$node);
if (
count($errs)) {
echo
$errs;
}
?>

Thank You - Newbie Question

irarab - July 19, 2009 - 20:03

I'm new to Drupal. This is exactly what I need.

Where exactly do I put this code?

Sorry to answer so late; you

pkej - October 28, 2009 - 22:28

Sorry to answer so late; you put it in your import code, which would be a specifically tailored php page.

You could also look into FeedAPI if your data is in XML.

Paul K Egell-Johnsen

To edit existing files

ncameron - September 3, 2009 - 15:14

The above code worked like a charm for us. We have a load of existing nodes all with the file specified in a text field and the file manually uploaded via FTP. We wanted to do a bulk update to include all those files in the drupal file system. The code below is a modification which opens up a node, adds a specified field, then saves it. The code is designed to be placed in a free standing php file in the root directory and then executed (make sure you do this securely).

<?php
   
require_once './includes/bootstrap.inc';
   
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

 
$mime = 'audio/mpeg'; // I was importing mp3 files

 
$file_drupal_path "sites/default/files/audio_files/file_name.mp3";


 
$file = new stdClass();
 
$file->filename = basename($file_drupal_path);
 
$file->filepath = $file_drupal_path;
 
$file->filemime = $mime;
 
$file->filesize = filesize($file_drupal_path);

 
$file->uid = '1';
 
$file->status = FILE_STATUS_PERMANENT;
 
$file->timestamp = time();
 
drupal_write_record('files', $file);
 
$file->fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath));
   
   
   
$nid = '123' // your NID here
   
 
$node = node_load($nid);

 
$node->field_audio_file = array(
    array(
     
'fid' => $file->fid,
     
'title' => basename($file->filename),
     
'filename' => $file->filename,
     
'filepath' => $file->filepath,
     
'filesize' => $file->filesize,
     
'mimetype' => $mime,
     
'description' => basename($file->filename),
     
'list' => 1,
    ),
  );

 
node_save($node);
?>

--
Web strategy and solutions, with a twist of Drupal
http://ndev.co.uk

 
 

Drupal is a registered trademark of Dries Buytaert.