Files are by default objects that live in the content folder, so if you really want a Files collection, you would have to create virtual files from those assets folder files: Virtual files | Kirby CMS
What is your use case?
Maybe it would be good enough to create a new Assets object from each file and pass that to a standard Collection object?
On my site there is a certain type of subpages that can be created from frontend. Each of these created subpages receives a randomly assigned 3D object. I now wanted to store these 3D files in the assets folder and on each page creation move one random one of them to the newly created subpage.
But this now seems quite complicated to me, I think I will rather create a hidden subpage that stores all these 3d files in its content folder and then they get picked and moved from there. I guess that would be much easier.
Not just to pick a random one, but also to have access to all the other methods. But I guess, there should be nothing wrong with moving them into the content folder altogether, right?
No, nothing wrong with that. But instead of moving them (and thub duplication them), you might as well assign a random object via a files field programmatically.
I don’t think, I quite understand …? How would I do that?
My plan is to move them without duplicating. Basically, I have a folder with a lot of files and whenever a subpage is created a random file is moved out of this folder into the subpages content folder.
Ah, ok, then forget what I wrote. Nevertheless it would be another option to have all files in a central folder and just assign them to page via a pages field. But that’s up to you.
The $kirby in front it is useless. At least as far as I can see at the moment. I’m not sure if this helps in this case but at least a first approach
$meshes = page('objects/meshes')->files();
But to be sure that the code runs cleanly, some queries should be made in advance.
$meshes = page('objects/meshes');
if ( $meshes ) {
if ( $meshes->hasFiles() ) {
echo 'Page was found and it has files, now make sure that the extension is correct ...';
} else {
echo 'Page was found but it has no files ...';
}
} else {
echo 'No page with that URL found';
}
Come to think about it, it probably makes more sense to move the file, because you want to get a random one out of the ones left in the folder.
@Oli1 Those if statements should be merged into one.
I wouldn’t subscribe to that. It’s two different things altogether. page() is a helper function, while in the case of $kirby->page(), page() is a method of the App class. The function and the method accept different parameters and their return values are also different.
In this case, using the page() helper is of course fine, if the page in question is no a draft.
That way I end up writing the file to a field, so I can later easily access it on my page. Not sure if this method makes sense. If it is too complicated, I would be very happy about hints how to improve it / make it easier!
Methods like $file->move() or $page->update() throw an error if the action fails for some reason. So you should wrap your code in a try/catch statement and handle the potential exception, for example. return an error message.