Help with Slim form

0 votes
561 views
asked May 25, 2016 in Slim by Saravanan

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
answered May 25, 2016 by Shamnadh
::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 235 views
asked Feb 9 in General by Marc Lentin
0 votes
1 answer 849 views
0 votes
1 answer 50 views
asked Aug 19, 2016 in General by Azi Graeber
0 votes
1 answer 224 views
asked Jul 11, 2016 in Magento by Graham G
0 votes
2 answers 132 views
...