I’m writing a page that gets games from an API that has the image url of a game. I want to download that image and store it in OctoberCMS. So far I have this, but I can’t figure out how to save the image in the correct folder for the media finder and how to link it to the model
Thanks for reporting your solution. We have improved the documentation related to this.
Uploading to Models
When working with models that are configured to use file attachments, including Tailor models that use the File upload widget, you save file uploads directly on the model.
The simplest approach is to set the attribute on the model directly using the files() helper. This supports singular and multiple file uploads.
function onUploadFiles()
{
$model = new MyModel;
$model->avatar = files('single_file');
$model->save();
// ...
Flash::success('File saved');
}
You may also set the attribute to a System\Models\File model object directly for various use cases.
$model->avatar = (new File)->fromFile('/path/to/somefile.jpg');
$model->avatar = (new File)->fromData('Some content', 'sometext.txt');
$model->avatar = (new File)->fromUrl('https://example.tld/path/to/avatar.jpg');