我一直在为瘦身框架中的android和ios app开发apis。最近我遇到了一个问题,我需要使用multipart / form-data上传视频或图像。
任何人都可以在苗条的微框架中为我提供一个例子吗?
答案 0 :(得分:0)
以下是我的简单示例:
HTML表单
<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!';
});