Hi,

media 7.x-2.x-dev
media_browser_plus
Drupal 7.9
PHP 5.3.6
Windows 2003 Server

I am attempting to write a very simple migration script to move files into Drupal from an old proprietary CMS and I have the files and a csv file with their attributes and associated data. There are several thousand files so I need to get some basic information and structure across in this migration and as such I want to use the folder structure and file naming. I now have a simple module which does all the easy bits but I'm stuck on populating the fields in Drupal.

An example of the code I have is as follows:

$file = 'c:/somewhere/file.pdf';

if (file_exists($file))
    {
        $dfile = (object) array(
			'uri' => $file,
			'filemime' => file_get_mimetype($file),
			'status' => 1,
			'folder' => 'Arts and entertainment',
			'title' => 'My First File',
			'tags' => 'Arts and entertainment'
		);
		        
        $drupalfile = file_copy($dfile, 'public://Arts and entertainment');

There is a folder setup called "Arts and entertainment" which has a physical entry in the file system and is recognised by Drupal.

What happens when I run this code is that the files are registered in Drupal and I can see them in both the thumbnail view and the list view BUT they are all in the media root folder (according to Drupal though the physical file is in the correct folder) and the name is set to the original name, not the supplied title.

The mimetype is set correctly.

I have spent a good couple of days googling around this in an attempt to understand it all and I suspect entities may come into it somewhere but while I had no problem figuring it out for user profiles I can't seem to figure it out for files.

I would use the media_browser_plus import feature but the folders import doesn't seem to work and there is no facility to change the titles of the files.

Any help (especially a working code sample or link to one ;) would be hugely appreciated.

cheers,

Tom

CommentFileSizeAuthor
#2 FileMigration.txt1.65 KBreujwils

Comments

specky_rum’s picture

Issue summary: View changes

corrected typo in example code.

specky_rum’s picture

Issue summary: View changes

found the physical files! All other facts remain valid.

clemens.tolboom’s picture

You should check drush, drush_entity commandline tooling and devel for code examples.

drush run-script gives you the drupal context so all code examples should work.

You could help us with drush entity #1330504: Test the File entity :) We should make drush entity-create file xyz work together.

Was this helpful?

reujwils’s picture

StatusFileSize
new1.65 KB

Hey Tom,

I just went through the process of migrating a site with about 4500 files along with other bits of content. What worked well for me was to use the Migrate module, which has a related project Migrate Module Extras which has a File migration type. It's quite easy to follow the examples of how to set up a migration using that, and I think it handles CSVs as well. Mapping the fields is also pretty straight forward. The one piece that seemed to be missing was the actually file copy in the migration, so my workaround was to zip up my files in the preexisting folder structure, drop them into the files directory that will be the home, and proceed with the migration. It did through informational messages saying it couldn't locate the destination, but everything worked without a hitch.

I'm attaching the Migration class I used for this, note though that my source was a database, however there are source types for xml and csv. Hope that helps.

Best of luck!

specky_rum’s picture

Thanks both for your suggestions. I was really after being able to do it using entities though, as much to understand how they work as anything else. I have yet to import nodes and may give the migrate module another look for that but I have managed to get the files import doing basically what I want with the following code:

// this code snippet is for a single entity.  Wrap it in a loop for multiple.
    // first check that the file exists
    if (file_exists($file))
    {
        // create a file object for Drupal
        $dfile = (object) array(
			'uri' => $file,
			'filemime' => file_get_mimetype($file),
			'status' => 1,
		);
		        
        // now get Drupal to copy it
        $drupalfile = file_copy($dfile, 'public://' . $folder_name . '/');
        
        // we should have a file object with an id.  The id is all we need
        $fid = $drupalfile->fid;
        
        // load the entity for this file (note, returns an array, indexed on fid
        $mediafiles = entity_load('file', array($fid));
        
        // set some attributes for the file and save those too
        // set the folder which is linked to a taxonomy term
        // check for a taxonomy term to get the ID
        $terms = taxonomy_get_term_by_name($folder_name);
        
        // we should have an array.  Find the value which is used by folders
        if (is_array($terms))
        {
        	foreach ($terms as $term)
        	{
        		if ($term->vocabulary_machine_name == 'media_folders')
        		{
        			$folderid = $term->tid;
                                break;
        		}
        	}
        }
                
        // set the file entity's folder
        if ($folderid) $mediafiles[$fid]->field_folder[LANGUAGE_NONE][0]['tid'] = $folderid;
       
        // change the filename so it uses something more meaningful
        $mediafiles[$fid]->filename = $file_name;
        
        // finally, save the attributes
        entity_save('file', $mediafiles[$fid]);
clemens.tolboom’s picture

(to both) Tnx for sharing ... hope to check our drush_entity with it :)

tsvenson’s picture

@djguvnor Nice work your doing there. Did you know the Media module comes with a feature to import files from the file system? Even thought hat feature copies the files to the root files system, it might contain some inspiration for the the code you're working on. You find that feature under /content/file/import.

specky_rum’s picture

tsvenson, I did know that but categorising the content is crucial for this work. The media browser plus module adds folders and allows you to import into folders but at the time I was doing this it didn't actually work properly!

Thanks all.

specky_rum’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

correction following further testing