# Images Object is empty after CURL Request

**URL:** <https://forum.getkirby.com/t/images-object-is-empty-after-curl-request/24184>\
**Category:** Questions\
**Tags:** v3\
**Created:** [December 8, 2021, 11:02am UTC](https://forum.getkirby.com/t/images-object-is-empty-after-curl-request/24184 "2021-12-08T11:02:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [December 8, 2021, 11:02am UTC](https://forum.getkirby.com/t/images-object-is-empty-after-curl-request/24184/1 "2021-12-08T11:02:54Z")

</div>

I load images via CURL into my Kirby instance. This works also wonderfully. Strangely the image is saved, but `$page->files()` remains empty. Only after a refresh the image is displayed. Does anyone happen to have an explanation for this?

```php
$filename = uniqid(rand(), true);
$saveto = $kirby->root('content') . '/test/' . $filename . '.jpg';
$url = 'https://example.com/image.jpg';

$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw = curl_exec($ch);
curl_close ($ch);
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);

dump($page->files());

```

---

<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 8, 2021, 11:34am UTC](https://forum.getkirby.com/t/images-object-is-empty-after-curl-request/24184/2 "2021-12-08T11:34:30Z")

</div>

That’s because at the time the script runs, the new files are not yet part of the files object. You would have to add the file to the files object first, or reload the page.

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [December 8, 2021, 11:43am UTC](https://forum.getkirby.com/t/images-object-is-empty-after-curl-request/24184/3 "2021-12-08T11:43:25Z")

</div>

Ahh ok! So you mean I should integrate: [new File() | Kirby CMS](https://getkirby.com/docs/reference/objects/cms/file/__construct)? Could that work? Or should I use [File::create() | Kirby CMS](https://getkirby.com/docs/reference/objects/cms/file/create)?

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [December 8, 2021, 12:20pm UTC](https://forum.getkirby.com/t/images-object-is-empty-after-curl-request/24184/4 "2021-12-08T12:20:07Z")

</div>

Ok, not very elegant but it works for now. Is there a way to overwrite the file with `File::create()`? Just adding a number and then deleting the old file isn’t exactly the best way I think, is it?

```php
$filename = uniqid(rand(), true);
$saveto = $kirby->root('content') . '/test/' . $filename . '.jpg';
$url = 'https://example.com/image.jpg';

$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw = curl_exec($ch);
curl_close ($ch);
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);

$kirby = kirby();
$kirby->impersonate('kirby');
File::create([
  'source' => $saveto,
  'parent' => $page,
  'filename' => $filename . '2.jpg',
  'template' => 'default'
]);
unlink($saveto);

```
