Community Documentation

Core Upload module to FileField

Last updated April 9, 2011. Created by gagarine on August 25, 2010.
Edited by roderik. Log in to edit this page.

This script migrates your data from upload module to fielfield.

A Drush script based on this code is also available.

Use with caution. Check that all the names of fields/tables etc. are correct.

<?php
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)) {
//print_r($file);
   
$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']);
print_r($attachment);
    if(
drupal_write_record('content_field_attachement', $attachment)==FALSE) {print "failed\n"; }
  }
}
_migrate_upload_to_filefield();
?>

Originally idea by http://castlin.net/blog/1/2009/12/migrating-d5-upload-module-d6-cck-file... and adapted by genox

Comments

You may need to rebuild

You may need to rebuild permissions after running this to see the files on the content. Also, if any of your files have a negative weight this process will fail.

How to run this code ?! Just

How to run this code ?! Just put it in a php file and visit it from FireFox ?!

Please help me get this php script working

HI,
I am not sure what to do here. My field name is fieldtest I do not know aht to edit in the php to convert upload module to filefield module.

If some one could help me to mod the script and tell me what I need to do to run it that would be wonderful

Thank you.

There is a module called

There is a module called "Devel". You can use it, add the accompanying php block to your theme and use it to launch the php code above.

Multiple file problem

Thanks for this snippet. However, it does not seem to cope with multiple files attached to a node. The delta attribute is used to discern these, so that the key consists of {nid, vid, delta, fid}.

Solved

I have solved this for my situation. I have inserted a counter, which is just incremented to distinct several files within one node. Note that the proper solution should be to restart the counter for every node, but this works and does not seem to have any side-effects.

function _migrate_upload_to_filefield() {
  $files = db_query("SELECT * FROM upload WHERE nid > 0 ORDER BY nid ASC, fid ASC");

  $nid = 0;
  $row=0;

  while ($file = db_fetch_array($files)) {
//print_r($file);
    $attachment = new stdClass();
$row++;
  
    // 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 = $row;
    $attachment->field_file_fid = $file['fid'];
    $attachment->field_file_list = $file['list'];
    // schema will take care of the serialization
    $attachment->field_file_data = array('description' => $file['description']);
print_r($attachment);
    if(drupal_write_record('content_field_file', $attachment)==FALSE) {print "failed\n"; }
  }
}
_migrate_upload_to_filefield();

restart counter

Using the previous solution you will have to much empty fields.
I tried converting 1000 uploads and the last node had 1000 empty filefields before the correct one. MySQL server gone away tring to edit the last node.

I needed a way for a lot of nodes with a lot of files, manual editing was forbidden.

So i found a simple solution checking if the node entering in the while cycle is the first or not.

I hope this helps.

function _migrate_upload_to_filefield() {
  $files = db_query("SELECT * FROM upload WHERE nid > 0 ORDER BY nid ASC, fid ASC");

  $nid=0;
  $row=0;
 
  $current=0;
 

  while ($file = db_fetch_array($files)) {
//print_r($file);
    $attachment = new stdClass();

$mynode = $file['nid'];

if ($current != $mynode){
$current = $file['nid'];
$row = 0;
echo 'This is a new NID. Delta is: '.$row.'\n';
}
else{
$row++;
echo 'Same nid. Delta is now: '.$row.'\n';
}

    // 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 = $row;
    $attachment->field_files_allegati_fid = $file['fid'];
    $attachment->field_files_allegati_list = $file['list'];

    // schema will take care of the serialization
    $attachment->field_files_allegati_data = array('description' => $file['description']);
print_r($attachment);
    if(drupal_write_record('content_field_files_allegati', $attachment)==FALSE) {print "failed\n"; }
 

 
  }
}
_migrate_upload_to_filefield();

--------------------------
www.syn-labs.it

Page status

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders, Site administrators

Structure Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.