Help with importing table of files into system_files and images into fileuploader

We are trying to import a db table with images into system_files - so that they will appear in the fileuploader field. Is there a best practice to do this? Using file uploader, Octobercms creates folder names based on the file name. We have thousands of images, so ideally, we would import the data into the system_files table and then copy the images. Is there a way to have octobercms just keep all of the images in one folder? Or to run a function/script that will get the images into correct folders?
Thanks in advance,
Jeremy

Hey Jeremy

Objects in the system_files table usually belong to some other model. Do you know which database model should be associated to each file?

Alternatively, you could just add these files to the storage/app/media directory for use with the media manager.

Best regards

Hi - basically, we have properties (houses), and each property has images. So the model is called Property. I can manipulate the csv file columns for import to include the appropriate data (content_type, field, attachment_type, attachment_id, etc.). The issue is uploading the images manually, which would be via ftp for production or copying on my local setup or development.

October creates folders based on the file name, so what is the best way to set this up so the files can be copied to a folder which October can access?

System_files might be an easier way to manage all this, because the old site has a similar setup for the images and the properties.

Thanks,
Jeremy

Dump all the files in the ~/temp/ directory. Make sure the disk names match up. This logic should get you started.

foreach (\System\Models\File::all() as $file) {
    // Valid file
    if (file_exists($file->getLocalPath())) {
        continue;
    }

    // Check alternative location (~/temp/...)
    $otherSource = base_path('temp').'/'.$file->disk_name;
    if (file_exists($otherSource)) {
        $file->fromFile($otherSource);
        $file->save();
    }
}

Building a command for it. First, create a plugin

php artisan create:plugin Acme.Migrator

Create a command

php artisan create:plugin Acme.Migrator MigrateFiles

Register the command (~/plugins/acme/migrator/Plugin.php)

public function register()
{
    $this->registerConsoleCommand('migrator.migrate-files', \Acme\Migrator\Console\MigrateFiles::class);
}

Write some migration logic (~/plugins/acme/migrator/console/MigrateFiles.php)

class MigrateFiles extends Command
{
    /**
     * @var string signature for the console command.
     */
    protected $signature = 'migrator:migratefiles';

    /**
     * @var string description is the console command description
     */
    protected $description = 'Migrates files from an alternative source';

    /**
     * handle executes the console command.
     */
    public function handle()
    {
        foreach (\System\Models\File::all() as $file) {
            // Valid file
            if (file_exists($file->getLocalPath())) {
                continue;
            }

            // Check alternative location (~/temp/...)
            $otherSource = base_path('temp').'/'.$file->disk_name;
            if (file_exists($otherSource)) {
                $file->fromFile($otherSource);
                $file->save();
            }
        }
    }
}

Run the command

php artisan migrator:migratefiles
1 Like

Thank you so much. I will give it a try and let you know.

Thanks again - made some adjustments and it worked. Much appreciated.

1 Like

You’re welcome. Glad to hear it worked out