File uploading in Rest Api using slim


#1

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


#2

You might have a look at this PR for an example.


#3

There is a good tutorial here that was published recently. It even provides a Unit Testing part.


#4

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


#5

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

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:

> 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

Hope that helps,

Lennaert


#6

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?


#7

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.


#8

Thanks. I will figure it out.


#9

For uploading files to S3 you can use flysystem package and it’s S3 adapter and for storing the credentials you can add them to your slim application configurations and then retrieve them via resolving the container within your controller or code.