Hi Texnixe,
that’s not exactly what i need. I need to import data from a database in Kirby. The client should manage the site from now on in Kirby. So i have a .json file whith the content and the images are in the json-file like {“images”:“image1.jpg,image2.jpg”} and i have those files temporarely stored in my assets folder. I create the pages with page::create and i want to create the images with the function i mentioned above. But sometimes it works, sometimes it fails with the same content… I’m struggeling a time now with this. It’s always a hard one to get data in Kirby from a database.
I know it’s a different use case, I was just suggesting to try the other method ($page->createFile() instead of File::create(), the source of the file shouldn’t really make any difference.
Ok, I read your first post again and if the images are created correctly if you pass a simple string, then there is probably a problem with how you get the array of image strings. How do you get them from your json?
It’s a very strange:
when i start with the a fixed array of images, it works
$images=[“169570_16752.jpg”,“169590_16795.jpg”];
foreach($images as $img){
…
when i start with a string and i make an array of it with code, it fails
$strimgs=“169570_16752.jpg,169590_16795.jpg”;
$images=explode(",",$strimgs);
foreach($images as $img){
…
Hm, I tested your code in a Starterkit (put the two images from one of albums into /assets/images, and the code snippet into the home template), like this:
The only thing strange about your code above are the quotes around the strings, but I assume that’s a copy/paste thing? They are not correct and throw an error when I copy your code.
Do you validate the images before throwing them into the File::create() method?
I am thinking about
trim()
file_exists()
Did you check the php error logs for any hints?
If the file already exists (in your page) the method will also fail. Something I usually run into when testing with files in a lot of subfolders and I forget to remove all of them.
i dumped the error (below the first lines of it). I think there’s something with the images i’m using (protected). I don’t know if i can do something so the images can be written?
Kirby\Exception\LogicException Object
(
[data:protected] => Array
(
)
[httpCode:protected] => 400
[details:protected] => Array
(
)
[isTranslated:protected] =>
[message:protected] => The file could not be created
[string:Exception:private] =>
[code:protected] => error.logic
[file:protected] => /Users/kristofgevaert/Sites/sanderuswebsite/kirby/src/Cms/FileActions.php
[line:protected] => 184
[trace:Exception:private] => Array
ok, thanks, i test this and it seems my file doesn’t exists (i get the error “does not exist”)… although the image is in my assets-folder.
The image i am using is corrupt i presume.