# Check if file is a png

**URL:** <https://forum.getkirby.com/t/check-if-file-is-a-png/11865>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 22, 2018, 12:34pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865 "2018-12-22T12:34:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@sun](https://forum.getkirby.com/u/sun)\
**Post date:** [December 22, 2018, 12:34pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/1 "2018-12-22T12:34:43Z")

</div>

How can I check if a file is a png? I’ve tried to use `extension()`, however, it is outputting something like ‘31ff35d1fe2a8941ac900f29fcea56d5.jpg’. Ideally, I would like to add ‘pdf’ to the classlist. Any help is greatly appreciated.

```
<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->thumbnail()): ?>
    <div class="<?= $thumbnail->extension(); ?>">

    </div>
    <?php endif; ?>
<?php endforeach; ?>

```

---

<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:** [December 22, 2018, 2:01pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/2 "2018-12-22T14:01:05Z")

</div>

I normally do something like this… you can repeat it for other extensions…

```auto
<?php foreach($labs->filterBy('extension', 'pdf') as $pdf): ?>
<a href="<?= $pdf->url() ?>"><i class="fa fa-file-pdf-o"></i><?= $pdf->filename() ?> (<?= $pdf->niceSize() ?>)</a> 
<?php endforeach ?>

```

I think yours isn’t working since its missing `file()`…

```auto
<?= $thumbnail->file()->extension(); ?>

```

It’s not clear from your code example how you are finding the files to work with. Where and how are you setting the `$labs` variable?

---

<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:** [December 22, 2018, 3:28pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/3 "2018-12-22T15:28:30Z")

</div>

Some notes here:

- Not quite clear if you want to check if the file is a `png` or a `pdf`, a thumbnail doesn’t make sense for a PDF.
- There is no `thumbnail()` file method, it is called `thumb()` (`thumbnail()` is a method of the Simpleimage class, but not of the file class)
- 

> [@jimbobrjames](#):
>
> \<?= $thumbnail-\>file()-\>extension(); ?\>

You probably mean `original()` not `file()`.

The above code should look like this:

```auto
<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->resize(200)): ?>
    <div class="<?= $thumbnail->original()->extension(); ?>">
blah blah
    </div>
    <?php endif; ?>
<?php endforeach; ?>

```

But if you want to filter by a particular extension, you have to ideally use a filter like suggested above, or use an if statement.

---

<div class="post-metadata">

**Author:** ![sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@sun](https://forum.getkirby.com/u/sun)\
**Post date:** [December 27, 2018, 4:39pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/4 "2018-12-27T16:39:13Z")

</div>

Apologies, that was badly worded. I do not want to filter by file type, just to output the file type (.png, .gif, .jpg, etc) in the containing `div`'s classlist, that way, I can change the container CSS based on the file type.

The more complete code is below, within the snippet `responsive-image` `toFile()` extension is called to create different size thumbnails. I added `original()` and the filename is still output rather than the extension type. Is what I’m trying to do possible using `extension()`?

```
<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->thumbnail()): ?>
    <div class="lab-thumbnail-cnt <?= $thumbnail->original()->extension(); ?>">
        <div class="lab-thumbnail responsive-image">
            <?php snippet('responsive-image', array('field' => $thumbnail)); ?>
        </div>
    </div>
    <?php endif; ?>
<?php endforeach; ?>

```

 ![39](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/3/32f82865748d2968122daea38f9012947e4a4a72.png)

---

<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:** [December 27, 2018, 5:24pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/5 "2018-12-27T17:24:47Z")

</div>

What is `thumbnail()`, a field in your page? Assuming that $labs is a collection of pages? Then that field only returns a filename, not a file object.

In that case, get the file like this:

```auto
<?php foreach($labs as $lab): ?>
    <?php if($thumbnail = $lab->thumbnail()->toFile()): ?>
    <div class="lab-thumbnail-cnt <?= $thumbnail->extension(); ?>">
        <div class="lab-thumbnail responsive-image">
            <?php snippet('responsive-image', array('field' => $thumbnail)); ?>
        </div>
    </div>
    <?php endif; ?>
<?php endforeach; ?>

```

---

<div class="post-metadata">

**Author:** ![sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@sun](https://forum.getkirby.com/u/sun)\
**Post date:** [December 27, 2018, 5:31pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/6 "2018-12-27T17:31:29Z")

</div>

Yes, `thumbnail()` is an image field and `$labs` is a collection.

If I add `toFile()` I get the error:

> Call to a member function extension() on null

```
<div class="lab-thumbnail-cnt <?= $thumbnail->original()->toFile()->extension(); ?>">
    <div class="lab-thumbnail responsive-image">
        <?php snippet('responsive-image', array('field' => $thumbnail)); ?>
    </div>
</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:** [December 27, 2018, 5:33pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/7 "2018-12-27T17:33:22Z")

</div>

See my code above please, not `original()->toFile()` but `thumbnail()->toFile()` within the if statement.

---

<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:25pm UTC](https://forum.getkirby.com/t/check-if-file-is-a-png/11865/8 "2025-02-06T17:25:34Z")

</div>


