I have a folder full of images that:
I have to upload to a series of pages of work
type
Using image
file template
And assign them to a files
field, titled cover
.
Each page has one cover
field, with a single image from the batch.
How do I programatically do this? Do I have to separate the upload file and update field code?
If there are docs for this, please help me find them
Thanks
These two are quite similar and should help you get going:
yes I have been going through the PHP classes but it is not working. let me try again with only those two params. I also tried this
$image = $page->createFile([
'source' => $external_url,
'parent' => $exhibit->parentModel(),
'filename' => $imageName,
'template' => 'exhibit_preview',
]);
but then it did not work either.
ok i just tried it and it is the same. no errors are thrown or anything, but the txt file is not there
c_exhibit isβ¦
Additionally, a files field expects data as an array, not a string
Also, at this point Kirby doesnβt know that this file exist yet as it is not in the inventory, so it wonβt be stored. You have to add it to the inventory first.
Also this recipe: Uploading files from frontend | Kirby CMS
1 Like
@texnixe I am getting a strange exception: The method: "alt" does not exist
with this code:
...
$img = asset('assets/import/img/' . $path . '.jpg');
if ($img->exists()) {
echo 'file ' . $img->filename() . ' exists, proceed to upload<br />';
try {
$w->createFile([
'source' => $img,
'filename' => $img->filename(),
'template' => 'focusimg',
]);
echo 'success <br />';
} catch (Exception $e) {
echo $e->getMessage() . '<br />';
}
} else {
"file does not exist <br /><br />";
}
It seems the exception is thrown when reaching the 'template' => 'focusimg'
line.
That blueprint looks like this:
title: focus image
accept: image/*
fields:
alt:
label: Texto alternativo
type: text
width: 1/1
focus:
label: Centro del recorte
type: focus
width: 1/2
Is it requiring me to fill the alt field using a content array on the createFile method? and if yes, why, if that is not a required field? or that is just a name coincidence between some internal alt
and my alt field?
Thanks
It seems my mistake was to use asset() to create the source, instead of simply providing the path as a string, this works:
$img = 'assets/import/img/' . $path . '.jpg';
if (file_exists($img)) {
echo 'file ' . $img . ' exists, proceed to upload<br />';
try {
$w->createFile([
'source' => $img,
'filename' => $path . '.jpg',
'template' => 'focusimg',
]);
echo 'success <br />';
} catch (Exception $e) {
echo $e->getMessage() . '<br />';
}
} else {
"file does not exist <br /><br />";
}