0 votes
1.8k views
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

<?php
$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
by
::src/routes.php

$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();
    $newfile->moveTo("/path/to/$uploadFileName");
}

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

Related questions

+2 votes
1 answer 1.3k views
asked Feb 9, 2017 in General by Marc Lentin
0 votes
1 answer 2.5k views
0 votes
1 answer 849 views
asked Aug 19, 2016 in General by Azi Graeber
0 votes
1 answer 1.2k views
asked Jul 11, 2016 in Magento by Graham G
0 votes
1 answer 649 views
asked Jan 23, 2022 in General by Sarath
0 votes
1 answer 604 views
asked Oct 29, 2021 in General by anonymous
...