# Image toFile() fails when name contains hyphen

**URL:** https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110
**Category:** Questions
**Tags:** v2
**Created:** [August 27, 2018, 3:40pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110 "2018-08-27T15:40:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![elhodel](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@elhodel](https://forum.getkirby.com/u/elhodel)
#### Post date: [August 27, 2018, 3:40pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110/1 "2018-08-27T15:40:32Z")

</div>

Hi!

Is it possible, that toFile() returns NULL when the filename contains a hyphen?

I have the following code:

```
if($mitarbeiter->portrait()->isNotEmpty()):
    $description = $mitarbeiter->name();
    $img = $mitarbeiter->portrait()->toFile();
    $border = $img->border()->value();

```

with an image named “test-image.jpg” i get the error

```
 Call to a member function border() on null

```

with an image named “test\_images.jpg” everything works.

Since Kirby sanitizes filenames on upload and leaves “-” i would expect, that this should work in both cases?

Tested on Kirby 2.5.4 and 2.5.12

Any ideas?

---

<div class="post-metadata">

### Author: ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)
#### Post date: [August 27, 2018, 3:50pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110/2 "2018-08-27T15:50:45Z")

</div>

Strange, I use images with hyphens in all the time, and don’t recall seeing an issue. What version of PHP are you on? What operating system is the site running on?

On a side note, you should check the image actually exists before using it. Checking the field has a value is not enough (the field will still have a value if the image has been physically deleted, unless you make a point of changing the field too).

---

<div class="post-metadata">

### Author: ![elhodel](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@elhodel](https://forum.getkirby.com/u/elhodel)
#### Post date: [August 27, 2018, 3:56pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110/3 "2018-08-27T15:56:11Z")

</div>

at the moment i’m on my localhost, with MAMP and PHP version 5.6.10

thanks for your remark, i will add this test.

---

<div class="post-metadata">

### Author: ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)
#### Post date: [August 27, 2018, 3:57pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110/4 "2018-08-27T15:57:40Z")

</div>

> [@elhodel](#):
>
> MAMP and PHP version 5.6.10

Try switching to PHP 7.x… 5.6 is still supported I think, but using 7.x is strongly encouraged.

The site I’m working on at the moment has images with hyphens in, running on Kirby 2.5.12 and PHP 7.x - works fine. I can’t test on MAMP because I use [Valet+](https://github.com/weprovide/valet-plus) for local development, although I don’t think its a MAMP issue.

---

<div class="post-metadata">

### Author: ![elhodel](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@elhodel](https://forum.getkirby.com/u/elhodel)
#### Post date: [August 27, 2018, 5:03pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110/5 "2018-08-27T17:03:04Z")

</div>

well it is working now…the hyphens weren’t the problem after all, but that i didn’t update the fields properly while testing. 😬 uuups!  
i realized that when i added the test to check if the image existed and started some more serious debugging… =)

so thanks for that advice!

---

<div class="post-metadata">

### Author: ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)
#### Post date: [February 6, 2025, 5:26pm UTC](https://forum.getkirby.com/t/image-tofile-fails-when-name-contains-hyphen/11110/6 "2025-02-06T17:26:23Z")

</div>


