Im sending appended data over a dropzone form. Now I got a problem with creating the file and adding it to the post.
I’m adding data via fetch over a routing and collection all filesname in an array. (createdImages)
Now when i press once submit all other values are updated but not the images.
When im checking the folder of i see all images and all txt files but the field “image” is empty in this post → When I press a second time the submit button in the fronted it works - image shows no in the backend of the post all files.
I think the problem is that the $submission page knows nothing of these files without a reload because you are using low level methods to save these files to disk without attaching them to the page inventory. And since Kirby doesn’t store images in a files field that do not exist, nothing is saved.
The “problem” is im getting the image as base64 and im creating the image on the fly.
So what I can do there? And why it attached it right on the second submit?
Is there any change to use base64 and $submission->createFile()
Ok, here is a basic working example, adapt to your code:
$createdFiles = [];
// the loop here is just for testing purposes
foreach([1,2] as $counter) {
$b64 = 'iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAAVElEQVR42u3PAQEAAAgCoPw/2h50IHhApu08EmFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYeHLAikKs4nmjczXAAAAAElFTkSuQmCC';
$decode = base64_decode($b64);
$props = [
'filename' => 'pixel' . $counter .'.jpg',
'parent' => $page,
];
// create a file object via the factory method
$file = File::factory($props);
// write content to this file
F::write($file->root(), $decode);
// add the file to the siblings collection
$file->siblings()->append($file->id(), $file);
// add id to array of created files
$createdFiles[] = $file->id();
}
// update page with array of file ids
$page->update(['image' => $createdFiles]);
It uploads the file but don’t create the txt file where the content should be based.
I can use a second write function but I thought i could also pass it here - or I’m wrong?