File paths on local server (MAMP)

Hi,

I’m having a problem with paths to images & files when I call $page->files() in my templates. They all seem to be absolute paths from my computer (/Users/name/work/kirby) rather than starting from the root of my virtual server (http://dev.kirbytest.de). Is there some configuration I need to set up to fix this? Because I seem to have missed it.

Thanks in advance.

Peter

You should get the correct url with the url() method: $file->url() when you look through the files.

Hi,

no unfortunately $page->files() gives me /Users/name/work/kirby/content/home-section/top-section/dino.jpg and then calling url() on the single file gives me nothing.

<?php foreach(page('home-section')->children()->invisible() as $home_section):
    $file = $home_section->files();
    echo $file; // this outputs /Users/name/work/kirby/content/home-section/top-section/dino.jpg
    echo $file->url(); // this fails silently
   endforeach; ?>

Thanks

Alright I see what I’ve done wrong here. I should be looping through the files, even if there is only one result. Underneath, the solution.

<?php foreach(page('home-section')->children()->invisible() as $home_section): ?>

  <?php

  $files = $home_section->files();

  foreach ($files as $file) {
    echo $file->url();
  }

  ?>
  
  <?php endforeach; ?>

Thanks for your help

Well, you are trying to echo an array of files which won’t work, even if it is only one file. Try this:

<?php foreach(page('home-section')->children()->invisible() as $home_section):
    $files = $home_section->files();
        foreach($files as $file) : 
           echo $file->url(); 
       endforeach;
   endforeach; ?>