0 votes
in Slim by

Hi, I'm noob to this framework. Trying new when I get some time in between. Please help me out with the below.

For posting from a form I do the following

$email = $app->request->post('email');

So, how can I post an image into a variable? I mean, how to receive an image from a form?

Please also let me know if any good tutorials are available for this online?

Thanks in advance.

1 Answer

0 votes

$app->post('/upload', function ($request, $response, $args) {
    $files = $request->getUploadedFiles();
    if (empty($files['newfile'])) {
        throw new Exception('Expected a newfile');

    $newfile = $files['newfile'];
    // do something with $newfile

To check that there is no error and to move the file to some other location, use this checking:
if ($newfile->getError() === UPLOAD_ERR_OK) {
    $uploadFileName = $newfile->getClientFilename();

There are other useful methods like getClientMediaType() and getSize() also. Please use as per your needs.

Related questions

+2 votes
1 answer 695 views
asked Feb 9, 2017 in General by Marc Lentin
0 votes
1 answer 1.7k views
0 votes
1 answer 191 views
asked Aug 19, 2016 in General by Azi Graeber
0 votes
1 answer 492 views
asked Jul 11, 2016 in Magento by Graham G
0 votes
1 answer 4 views
0 votes
1 answer 3 views
asked Jun 2 in General by mithun (870 points)