# File uploading in Rest Api using slim

**URL:** <https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606>\
**Category:** Questions\
**Created:** [June 22, 2017, 10:28am UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606 "2017-06-22T10:28:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![noumanyasin](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@noumanyasin](https://discourse.slimframework.com/u/noumanyasin)\
**Post date:** [June 22, 2017, 10:28am UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/1 "2017-06-22T10:28:53Z")

</div>

Is there any demo or code available for file uploading in rest Api. I am new to slim and Rest Api. Any kind of help will be appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [June 22, 2017, 12:55pm UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/2 "2017-06-22T12:55:04Z")

</div>

You might have a look at [this PR](https://github.com/slimphp/Slim-Website/pull/233) for an example.

---

<div class="post-metadata">

**Author:** ![kalvn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kalvn/32/443_2.png) [@kalvn](https://discourse.slimframework.com/u/kalvn)\
**Post date:** [June 28, 2017, 1:37pm UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/3 "2017-06-28T13:37:24Z")

</div>

There is a [good tutorial here](https://mhdzaherghaibeh.name/2017/06/19/testing-upload-file-with-slim-3-a-better-way/) that was published recently. It even provides a Unit Testing part.

---

<div class="post-metadata">

**Author:** ![noumanyasin](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@noumanyasin](https://discourse.slimframework.com/u/noumanyasin)\
**Post date:** [June 29, 2017, 8:50am UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/4 "2017-06-29T08:50:53Z")

</div>

How does this function work for Rest Api? Also I need to send uploaded file into database.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [June 29, 2017, 9:01pm UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/5 "2017-06-29T21:01:00Z")

</div>

For a simple file upload, you can upload a file by sending a PUT or POST request and putting the file contents in the request body. On the receiving side (Slim), you then store the content.

Below is an example:

```php
<?php

require_once __DIR__. '/vendor/autoload.php';

use Slim\Http\Request;
use Slim\Http\Response;

$app = new Slim\App();

$app->post('/upload', function (Request $request, Response $response) {
    $id = bin2hex(random_bytes(16)); // generate a unique id

    // optionally send the content type in the header
    $contentType = $request->getHeader('Content-Type') ?? 'application/octet-stream';

    // the request body is the content of the file
    $data = $request->getBody()->getContents();
    file_put_contents($id, $data);

    // return some information on the file we just stored
    return $response->withJson([
        'id' => $id,
        'content-type' => $contentType,
        'content-length' => strlen($data)
    ]);
});

$app->run();

```

And calling this REST API endpoint:

```auto
> curl --silent -X POST --data-binary @test.png -H "Content-Type: image/png" localhost:8000/upload | aeson-pretty 
{
    "content-type": [
        "image/png"
    ],
    "id": "11e2ab9ce40408396d887530cb14c3d2",
    "content-length": 366283
}

```

Note that this example loads the complete uploaded file into memory before storing the data. It will therefore not work for very large files.

Storing the uploaded file content in a database is not Slim specific. Personally, I prefer to not store file contents in the database, and only store metadata such as title, relative path on disk, content type, user id and date of upload. It makes it faster to make backups and easier to move those around.

You can look at existing REST APIs and see how those handle file uploads. For example [https://developers.google.com/drive/v3/web/manage-uploads](https://developers.google.com/drive/v3/web/manage-uploads)

Hope that helps,

Lennaert

---

<div class="post-metadata">

**Author:** ![noumanyasin](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@noumanyasin](https://discourse.slimframework.com/u/noumanyasin)\
**Post date:** [July 3, 2017, 1:27pm UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/6 "2017-07-03T13:27:24Z")

</div>

Have you ever tried to **save files to Amazon S3 Bucket from slim**?I have done this by simple php but not in slim.Another thing is by uploading files on s3 we only need to save our file path to mysql database?

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [July 3, 2017, 6:19pm UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/7 "2017-07-03T18:19:40Z")

</div>

If you have managed uploading files to an S3 bucket with plain PHP, then you should be able to do so as well with Slim.

I have not worked with Amazon S3 before. I guess storing the bucket and key name in your database should suffice to retrieve the data from S3 later.

---

<div class="post-metadata">

**Author:** ![noumanyasin](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@noumanyasin](https://discourse.slimframework.com/u/noumanyasin)\
**Post date:** [July 4, 2017, 5:24am UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/8 "2017-07-04T05:24:38Z")

</div>

Thanks. I will figure it out.

---

<div class="post-metadata">

**Author:** ![zaher](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/zaher/32/442_2.png) [@zaher](https://discourse.slimframework.com/u/zaher)\
**Post date:** [July 9, 2017, 7:50am UTC](https://discourse.slimframework.com/t/file-uploading-in-rest-api-using-slim/1606/9 "2017-07-09T07:50:14Z")

</div>

For uploading files to S3 you can use [flysystem](http://flysystem.thephpleague.com) package and it’s S3 [adapter](https://flysystem.thephpleague.com/adapter/aws-s3-v3/) and for storing the credentials you can add them to your slim application [configurations](https://www.slimframework.com/docs/objects/application.html#application-configuration) and then retrieve them via resolving the container within your controller or code.
