上传文件Slim 3

时间:2017-01-26 06:28:24

标签: php slim

这是我通过GET向控制器发布数据的方式     

use Slim\Views\Twig as View;

class  place_pub_controller extends BaseController{

function place_pub($request , $response){
     $arr_requests=$request->getParams();
     $id=$arr_requests['prop_id'];
     $file=$arr_requests['my_file']; //output [object file]

Html FORM

<form id="frm_pub"  method="GET" enctype="multipart/form-data">

    <div class="container"></div> 
</form>

 <input type="file"> is placed dynamically within div class="container"

我无法获取该文件。我该怎么办?

1 个答案:

答案 0 :(得分:4)

Slim3也使用PSR-7文件,因此您应该在请求中使用上传的文件功能

function place_pub($request, $response) {
    $files = $request->getUploadedFiles();
    if (empty($files['my_file'])) {
        throw new Exception('No file has been send');
    }
    $myFile = $files['my_file'];
    if ($myFile->getError() === UPLOAD_ERR_OK) {
        $uploadFileName = $myFile->getClientFilename();
        $myFile->moveTo('uploads/' . $uploadFileName);
    }
}

查看UploadedFileInterface以获取有关该文件的功能的更多信息。