Panel hook to create text file with all file names returns unwanted strings

Hi there,

I am using a hook to create a text file with all files on a page. It’s working fine so far, except the resulting images.text file contains a bunch of unwanted strings… I am pretty unfamiliar with php file creation, so I don’t know if it’s a usual thing, but I would anyway want to remove them from the file.

Here’s what my hook inside the config looks like:

kirby()->hook([''], function($page) {
    $content = array();

    $content[] = $page->title()->toString() . "\n" . $page->artists()->toString() . "\n";

    $i = 0;
    foreach ($page->images() as $image) {
      $image->rename(sprintf("%02d", $i) . '_Pina_' . substr($page->title()->toString(), 0, 25));
      $content[] = $image->filename() . " — " . strip_tags($image->caption()->kirbytext()) . "\n";
    f::write($page->root() . '/images.text',
    $append = false);

This is the resulting file:

a:11:{i:0;s:31:"Staged Stairs
Emanuel Rossetti
";i:1;s:48:"01_pina_staged-stairs.jpg — Installation View
";i:2;s:48:"02_pina_staged-stairs.jpg — Installation View
";i:3;s:83:"03_pina_staged-stairs.jpg — Staged Stair II, 2019, self adhesive vinyl, 270x90cm

What do these a:11:{i:0;s:31:" mean and how to remove them? Maybe there’s an error in my hook function…

Thanks in advance!

This is because you’re trying to store an array, not a string, so your array is automatically serialized.

oh, that entirely makes sense. using implode() in my write function now & it’s fixed. Thanks! :~)