Multipart/form-data example in slim micro framework

筅森魡賤 提交于 2020-01-02 07:03:24

问题


I have been developing apis for android and ios app in slim framework. Recently i came across a problem where i need to upload videos or images using multipart/form-data.

Can any one provide me an example of it in slim micro framework?


回答1:


Here is my simple example:

HTML Form

<form enctype="multipart/form-data" action="/uploadPhoto" method="POST">
    Select photo: <input name="photo" type="file" />
    <input type="submit" value="Upload" />
</form>

PHP:

$app->post('/uploadPhoto', function () use ($app) {

    $uploadDir = '/full/path/to/upload/dir/';
    $error = false;

    if (empty($_FILES)) {
        echo 'No files';
        $app->stop();
    }

    if (!empty($_FILES['photo']) && $_FILES['photo']['error'] !== 4) {
        if (!$_FILES['photo']['error']) {
            if (!move_uploaded_file($_FILES['photo']['tmp_name'], $uploadDir.$_FILES['photo']['name'])) {
                echo 'There is a error while processing uploaded file';
                $app->stop();
            }
        } else {
            echo 'Error while uploading file';
            $app->stop();
        }
    }

    echo 'Success!';
});


来源:https://stackoverflow.com/questions/26192698/multipart-form-data-example-in-slim-micro-framework

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!