# File uploads: How?

**URL:** <https://discourse.slimframework.com/t/file-uploads-how/4715>\
**Category:** Questions\
**Created:** [March 21, 2021, 7:27pm UTC](https://discourse.slimframework.com/t/file-uploads-how/4715 "2021-03-21T19:27:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FatboySlim](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@FatboySlim](https://discourse.slimframework.com/u/FatboySlim)\
**Post date:** [March 21, 2021, 7:27pm UTC](https://discourse.slimframework.com/t/file-uploads-how/4715/1 "2021-03-21T19:27:51Z")

</div>

Hi,

I’m trying to upload a file using postman ([following the docs](https://www.slimframework.com/docs/v4/cookbook/uploading-files.html)) and I’m getting an error at this line:

` $directory = $this->get('upload_directory');`

I expect this relates to the container, in my settings file I have this:

`//Upload directory`  
`$settings['upload_directory'] = $settings['root'] . '/uploads';`

**How can I get the `upload_directory` into my `__invoke` method?**

**Also is `UPLOAD_ERR_OK` defined anywhere, doesn’t look like it. How/where should this be defined?**

```
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{

    $supplier = (string)$args['supplier'];

    $responseData = ['supplier' => $supplier];

    $directory = $this->get('upload_directory');
    $uploadedFiles = $request->getUploadedFiles();

    // handle single input with single file upload
    $uploadedFile = $uploadedFiles['supplier'];
    if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
        $filename = $this->moveUploadedFile($directory, $uploadedFile);
        $responseData = [
            'filename' => $filename,
            'supplier' => $supplier,
            'message' => 'File uploaded'
        ];
        //$response->getBody()->write('Uploaded: ' . $filename . '<br/>');
    }

    // Invoke the Responder and pass in the response and responseData ($responseData)
    return $this->responder->withJson($response, $responseData);

}
```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [March 21, 2021, 8:53pm UTC](https://discourse.slimframework.com/t/file-uploads-how/4715/2 "2021-03-21T20:53:20Z")

</div>

The Slim documentation shows an example of retrieving the settings inside a route callback (closure).

If you want to retrieve the settings inside a class, you may better pass the settings via the constructor. Here you can find a tutorial: [Slim 4 - Configuration](https://odan.github.io/2021/03/21/slim4-laminas-config.html).

The `UPLOAD_ERR_OK` constant is a native PHP constant. You don’t have to define it, it’s already defined. See here: [Error Messages Explained](https://www.php.net/manual/en/features.file-upload.errors.php#features.file-upload.errors)

---

<div class="post-metadata">

**Author:** ![FatboySlim](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@FatboySlim](https://discourse.slimframework.com/u/FatboySlim)\
**Post date:** [March 21, 2021, 10:13pm UTC](https://discourse.slimframework.com/t/file-uploads-how/4715/3 "2021-03-21T22:13:48Z")

</div>

Awesome, thank you so much! I’ll take a look at that.

---

<div class="post-metadata">

**Author:** ![FatboySlim](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@FatboySlim](https://discourse.slimframework.com/u/FatboySlim)\
**Post date:** [March 22, 2021, 8:15pm UTC](https://discourse.slimframework.com/t/file-uploads-how/4715/4 "2021-03-22T20:15:15Z")

</div>

@odan Hey, got it working now, thank you so much! Never been so happy to see a JSON response!
