0 votes
2.1k 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.5k views
asked Feb 9, 2017 in General by Marc Lentin
0 votes
1 answer 2.8k views
0 votes
1 answer 1.1k views
asked Aug 19, 2016 in General by Azi Graeber
0 votes
1 answer 1.4k views
asked Jul 11, 2016 in Magento by Graham G
0 votes
1 answer 1.1k views
asked Jan 23, 2022 in General by Sarath
0 votes
1 answer 1.0k views
asked Oct 29, 2021 in General by anonymous
...