Image toFile() fails when name contains hyphen

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?

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).

1 Like

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.

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+ for local development, although I don’t think its a MAMP issue.

well it is working now…the hyphens weren’t the problem after all, but that i didn’t update the fields properly while testing. :grimacing: 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!

1 Like