# Unexpected error with file object url inside loop

**URL:** <https://forum.getkirby.com/t/unexpected-error-with-file-object-url-inside-loop/18713>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 6, 2020, 5:37am UTC](https://forum.getkirby.com/t/unexpected-error-with-file-object-url-inside-loop/18713 "2020-07-06T05:37:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjarte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bjarte/32/10777_2.png) [@bjarte](https://forum.getkirby.com/u/bjarte)\
**Post date:** [July 6, 2020, 5:37am UTC](https://forum.getkirby.com/t/unexpected-error-with-file-object-url-inside-loop/18713/1 "2020-07-06T05:37:24Z")

</div>

I am upgrading one of our sites from Kirby 2 to Kirby 3 and are having a problem with a foreach loop in one of my snippets.

The code below is a snippet for a gallery that is part of a one-pager.  
For some reason \<?= $project\_image-\>url() ?\> inside the foreach loop breaks the page.  
Outside the loop the url prints just fine.

I have used similar code in my other snippets, and there it works just fine. It just doesn’t work in this one particular snippet.  
In addition the site reports no php errors which makes it incredible hard to debug. I am not sure if this is because of some interaction between Kirby and the php error log I am not aware of. But have tried added error reporting in both php and apache.

Really stuck here, and any help would be much appreciated.

```
<div id="projects" class="section" >

<div class="sectioncontent">

<h1><?= $data->title()->html() ?></h1>

        
        <div class="projectgallery">

        <?php foreach($data->children()->listed() as $project): ?>

            <?php // Convert the filename to a full file object
                $project_image = $project->projectimage()->toFile(); ?>
            <a href="<?php echo $project->url() ?>">
                <div class="projectThumb" style='background-image: url(<?= $project_image->url() ?>)'>
               
                    <div class="projectInfo">
                        <h1><?php echo $project->title()->html() ?></h1>
                        <p><?php echo $project->clientName()->html() ?></p>

                        <div class="ProjectText">
                        <?php echo $project->summary()->kirbytext() ?>
                        </div>
                    </div>
                    <div class="projectThumbOverlay">
                    </div>
                </div>
            </a>
        <?php endforeach ?>  

</div>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 6, 2020, 7:01am UTC](https://forum.getkirby.com/t/unexpected-error-with-file-object-url-inside-loop/18713/2 "2020-07-06T07:01:12Z")

</div>

There’s an if statement missing that makes sure `$project_image` is an image before calling the `url()` method:

```auto
<?php $project_image_url = ($image = $project->projectimage()->toFile() ) ? $image->url() : ''; ?>
      <div class="projectThumb" style="background-image: url(<?= $project_image_url ?>)">

```

---

<div class="post-metadata">

**Author:** ![bjarte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bjarte/32/10777_2.png) [@bjarte](https://forum.getkirby.com/u/bjarte)\
**Post date:** [July 6, 2020, 7:38am UTC](https://forum.getkirby.com/t/unexpected-error-with-file-object-url-inside-loop/18713/3 "2020-07-06T07:38:58Z")

</div>

Thank you so much!
