Community Documentation

File migration

Last updated February 3, 2013. Created by mikeryan on October 22, 2012.
Edited by DanChadwick, alexweber. Log in to edit this page.

The simplest and most efficient way to get your files imported is to use the distinct file migration class, rather than try to map them directly in the referencing classes. These classes basically map the source files (or file_managed) table to the destination file_managed table, copying referenced files as necessary. The supported arguments:

  • user_migration: The machine name of your user migration, used to properly assign ownership of the files.
  • default_uid: A default destination uid to use as the default owner of files where a legacy owner isn't identified (for example, files owned by the account with uid 1 in the legacy system, since that account is not migrated).
  • source_dir: The source destination from which to copy migrated files. See here for more info.
  • destination_dir: The destination to which to copy migrated files. This defaults to public://.
  • file_class: An override for the default MigrateFileUri class - you would use this if you had extended that class with application-specific behavior.

<?php
$file_arguments
= $common_arguments + array(
 
'machine_name' => 'ExampleFile',
 
'description' => t('Import Drupal 6 files'),
 
'user_migration' => 'ExampleUser',
 
'default_uid' => 1,
 
'source_dir' => '/path/to/files',
 
'destination_dir' => 'public://legacy_files',
);
Migration::registerMigration('DrupalFile6Migration',
 
$file_arguments['machine_name'], $file_arguments);
?>

Using this strategy, files are migrated before being used in a subsequent migration, such as for a node containing a referenced image field. In the (custom) node migration, the referenced file should be migrated with a field mapping specifying a file_class of MigrateFileFid. This migrates only the reference to the file, not the file itself. Additionally, preserve_files should be set to TRUE to avoid deleting the actual file, should the node migration be rolled back (requires Migrate 7.x-2.6 or later). The mapping in the constructor for the node migration might contain:

<?php
    $this
->addFieldMapping('field_my_image', 'field_my_source_image')
         ->
sourceMigration('MyFileMigrationMachineName');
   
$this->addFieldMapping('field_my_image:file_class')
         ->
defaultValue('MigrateFileFid');
   
$this->addFieldMapping('field_my_image:preserve_files')
         ->
defaultValue(TRUE);
   
$this->addFieldMapping('field_my_image:title', 'image_copyright')
         ->
defaultValue(t('(c) 2012 My Site'));
   
$this->addFieldMapping('field_image:alt', 'image_description')
         ->
defaultValue('');
   
$this->addFieldMapping('field_my_image:language')
         ->
defaultValue(LANGUAGE_NONE);
?>

Comments

Source dir mandatory?

Hi there,

I'm really struggling with getting this working in a pretty simple case. I'm attempting to move all files from a D6 installation to a D7 installation. I notice that the comments in migrate_d2d/file.inc suggest that the "source_dir" argument is mandatory, but it's not in your example, nor can I see anywhere in the code that it is set to a default value.

Having said that, I've tried setting the source_dir to pretty much every reasonable value I can think of, and I always get an error like this for each file:

The specified file /home/groupanizer/domains/misc/staging/sites/mustsingit.misc.staging.groupanizer.com/files/previews/donya.metzger/45/I Don't Know Enough About You_0.mp3 could not be copied to public://legacy_files/previews/donya.metzger/45/I Don't Know Enough About You_0.mp3

In the above case I was trying to set the path to the exact file system path, but I also tried relative paths and URL's.

What am I doing wrong?

Thanks,
Tom

I updated the page with info

I updated the page with info on the source_dir param.

Source dir should not include "sites/default/files"

It assumes that, so make it the path to your drupal installation.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate, Advanced
Drupal’s online documentation is © 2000-2013 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.