1

I am trying to create say "Articles" manually using scripts. Basically I am reading data from mysql table and creating nodes programmatically by looping the tables.

However in mysql content field which is the Body field of my Article content type has the data with inline embedded images and links.

<p><img src="/assets/default/85/858dee4200c7f440cd6d4dba8814d56a8e81ab1f/original.jpg"></p><h1> Some more dummy text.</h1>

What would be the best way to migrate images for such cases?

1

First off, there really can't be a best way without describing any requirements that can be objectively measured (e.g. speed, loc, number of steps, etc.). However, you can do a few things to do use those inline files in Drupal.

First thing is to sync (e.g. rsync) your content file dir under Drupal's publically managed file system. Depending on your organizational preference, this can be:

  • The default path (sites/default/files from your Drupal root).
  • A subdir in the default path (e.g. sites/default/files/assets)
  • Or a completely arbitrary dir; you can use any directory in Drupal root to be the public file system by configuring it in the admin/config/media/file-system admin page. In fact, you could make it the same directory as your previous CMS (assets/default).

If you decide to use a new directory (i.e. not assets/default) you'll need to either:

  • Create web server redirect rules (e.g. Apache .htaccess RewriteRule) from the old URLs to the new URLs.
  • Find old URL references in your imported content and replace them with the new URL references.

The first option is the better of the 2 since it accommodates other caches of your documents (e.g. cached web crawler versions of your content) that you can't update and it's less error prone (e.g. no breaking content because regex mismatches).

The last thing to mention is programmatically inserting records into Drupal's managed files system (its file_managed DB table). This isn't required for your imported articles to display, but if you ever want to manage the imported files through various Drupal file-management tools, it will be needed.

Since you're already using a PHP script to programmatically, you can add to the script a function to iterate over all sync'd files in the public directory and save them to Drupal.

//Assume $files is an array of file paths relative to the new public directory 
//(e.g. 85/858dee4200c7f440cd6d4dba8814d56a8e81ab1f/original.jpg)
foreach ($files as $filepath) {
  $uri = "public://$filepath";
  $file = file_save((object) array(
    'filename' => basename($uri), 
    'uri' => $uri, 
    'status' => FILE_STATUS_PERMANENT, 
    'filemime' => file_get_mimetype($uri))
  );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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