tirta keniten tirta keniten - 7 months ago 135
PHP Question

Slim Framework 3 Upload

I have problem upload files through slim framework 3 Slim\Http\UploadedFile.

My code:



$app->post('/upload', function ($req, $res, $args) {
$setting = $this->settings;
$uploadPath = $setting['upload']['path'];
$file = $req->getUploadedFiles()['img'];
$file->moveTo($uploadPath);
return $res;
});





Result:



Slim Application Error
The application could not run because of the following error:

Details

Type: RuntimeException
Message: Error moving uploaded file hss.png to /home/xxx/web/slim3/app/../log
File: /home/xxx/web/slim3/vendor/slim/slim/Slim/Http/UploadedFile.php
Line: 237




Answer

I already found out the answer. Thanks to @akrabat

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Slim 3</title>
        <link rel="stylesheet" href="http://yegor256.github.io/tacit/tacit.min.css">
    </head>
    <body>
        <h1>Upload a file</h1>
        <form method="POST" action="/upload" enctype="multipart/form-data">
            <label>Select file to upload:</label>
            <input type="file" name="newfile">
            <button type="submit">Upload</button>
        </form>
    </body>
</html>

$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
});