5

I'm in the progress of creating a bulk upload function for a Drupal site. Using flash I'm able to upload the files to a specific url that should be able to handle the files.

What I want to do, is not just to upload the files, but create a node of a specific type with the file saved to a file-field that has been setup with CCK. Since these are audio files, it's important that file-field handles the files, so additional meta data can be provided with the getid3 module.

I've taken a look at the code/doc, but wasn't able on the first round to figure out how to handle this. Ideally, there is some function that I've missed, that can handle most of this for me.

If any one has experience with this I would apreciate some pointers on how to approach this matter.

1 Answer 1

5

It's pretty straight forward, setup your content type with the required fields, then you want to use code similar to this:

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

$file->uid = $uid;
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.