I would like to switch from the core Upload module to FileField, but I already have file attachments which were uploaded via the core module. Can someone please give me some hints on how can I do this migration automatically, using a PHP script and the information already stored in the database?
I've created a FileField field for the content type I have, and I've looked at the "upload" table and the "content_field_CONTENT_TYPE_attachments" table created by CCK, and the information looks similar, so I tried to add a new row to the content_field_CONTENT_TYPE_attachment table, based on the info in one of the "upload" table rows, but the attachment does not appear on the page, so I guess I need to do some other modifications as well.

Can anyone give me some pointers?

Thank you.

#7 ohai.drush_.inc_.gz1.21 KBkaare


katiusha’s picture

Component: Documentation » Code
Category: feature » support
quicksketch’s picture

There are several examples of how to migrate from other modules (Image, Audio, Node Images) in the FileField Handbook: http://drupal.org/node/432852. I'd suggest trying to adapt one of those examples to work with Upload module. When finished, it would be helpful to others in the future to add your own solution as a new page to that handbook section.

stacysimpson’s picture

Did you ever put together a script?

quicksketch’s picture

Status: Active » Closed (fixed)

Closing after lack of activity.

juan.bangkok’s picture

I'm in the same situation as the OP. I've been looking for a script to do this in the forums without success. If there isn't one I'll try to code it and post it here before for review (it'll be my first PHP script *ever*). Otherwise it'd be great to know where to find it to avoid reinventing the wheel.

genox’s picture

Hey there,

I stumbled across this script here: http://castlin.net/blog/1/2009/12/migrating-d5-upload-module-d6-cck-file...

Basically, it does what you want. However, you have to check that all the names of fields/tables etc. are correct. I had to correct some typos too. Here's the version I used to migrate all my files from Upload to FileField:


function _migrate_upload_to_filefield() {
  $files = db_query("SELECT * FROM upload WHERE nid > 0 ORDER BY nid ASC, fid ASC");
  $nid = 0;
  while ($file = db_fetch_array($files)) {
    $attachment = new stdClass();
    // D5 didn't have weights, 
    // so we'll store things in the order they were uploaded
    if ($nid != $file['nid']) {
      $delta = 0;
      $nid = $file['nid'];
    $attachment->vid = $file['vid'];
    $attachment->nid = $file['nid'];
    $attachment->delta = $file['weight'];
    $attachment->field_attachement_fid = $file['fid'];
    $attachment->field_attachement_list = $file['list'];
    // schema will take care of the serialization
    $attachment->field_attachement_data = array('description' => $file['description']);
    if(drupal_write_record('content_field_attachement', $attachment)==FALSE) {print "failed\n"; }


Use with caution - no warranties etc. :-)

kaare’s picture

1.21 KB

I've recently done this myself, and took this script and refined it a bit. It now only converts files to content types that has a valid filefield for attachment. It's a drush script and can be run as many times as desired.

Put it in ~/.drush as ~/.drush/ohai.drush.inc and run drush ohai-convert from your host.

roderik’s picture

Downloaded the script in #7. Ran it. Like it.

Note to casual readers entering at comment #7:

- you need to make sure that the FileField is already added to your content type (obviously)

- you need to edit the file, to change the field name of your FileField, at the top of the code. (Unless it's "field_attachment").

- if you have multiple content types with different field names set up, that's fine. Change the field name at the top of the script to "field_attachmentname-in-type-1" and run it; part of the attachments will get converted. Then cange the field name to "field_attachmentname-in-type-2" and run it again.

SpriteGF’s picture

For those searching, there's additional discussion on migrating Upload to FileField here in the Drupal Handbook: http://drupal.org/node/893074

rv0’s picture

great script in #7

one note though, on line 24

  $files = db_query("select * from upload where nid > 0 and vid > 0 order by nid, vid, weight, fid");

change that to

  $files = db_query("select * from {upload} where nid > 0 and vid > 0 order by nid, vid, weight, fid");

Otherwise it wont work with table prefixes..
Other than that, it works perfectly, converted 100s of nodes in no-time.

more comments:
- it doesnt clean out the upload table afterwards.
- it doesnt move the files to the filefields upload path.

Encarte’s picture


kmillecam’s picture

Regarding the Drush script provided in comment #7, I'm getting this error:

Drush command terminated abnormally due to an unrecoverable error.                       [error]
Error: Call to undefined function db_fetch_array() in
/home/iade118/.drush/ohai.drush.inc, line 27
rv0’s picture

the script is for drupal 6..
you are using drupal 7?

Kniekel’s picture

Issue summary: View changes

OK, I'm a bit late - currently switching my last D6 site to D8 - but I must say: Thank you. #7: This. Is. Fantastic. Worked like a charm.