This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

TJMB
TJMB

Hello,

I'm rebuilding a website for a client of mine, moving from an outdated backend to October. Part of my reason for choosing October is that it allows me to use the site's existing database (therefore no need to restructure/rebuild a large database of products and content).

In the existing data table for the site's products, I have fields named image_1 through image_6 that store the filename of images uploaded for each record. For the October version of the site, I've set these up as fileupload fields in image mode. What I'd like to be able to do is to take the existing filename data from the products table and translate that to image attachments for each record.

Example:

  • Record 8327 in the products table has a value of DSCN5289.jpg in the image_1 field.
  • The path to the corresponding image file on the server is /public_html/interface/images/items/DSCN5289.jpg

How can I take data and files stored this way, loop through the products data, and convert each of the image_N fields into attachment data that October can utilize?

Is there a utility or a script already available to accomplish something like this? Or at least someone who can get me pointed in the right direction?

Thanks in advance for any help!

netsmertia
netsmertia

The question is very old but may help someone hitting on this issue when migrating old data into new schema with octobercms. Move data is pretty easy but Octobercms rename all the files and store the files in hash characters directories like "5e/2b/23bl23b209.jpeg".

The solution is to create a seeder that upload the files/images from local directory and insert a relation record in to system_files table.

        $recipes = Recipe::get();
        foreach($recipes as $recipe) {
            $imageCode = $recipe->thumb;
            if ($imageCode) {
                $imageCodeParts = explode("/", $imageCode);
                $imageName = $imageCodeParts[count($imageCodeParts) - 1];
                // images are stored at /plugins/Nets/Recipes/images
                $imagePath = __DIR__ . "/../images/" . $recipe->id . ".jpg";
                // create a file object
                $file = new File();
                $imageModel = $file->fromFile($imagePath);
                $imageModel->save();
                // img is attachment on Recipe model
                $recipe->img()->add($imageModel);
            }
        }

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.