简体   繁体   English

多个文件上传苗条框架仅运行一次

[英]Multiple file upload slim framework only runs once

I'm trying to upload multiple files and save their path to a database using the slim framework. 我正在尝试使用slim框架上载多个文件并将其路径保存到数据库。 The problem is that when I test the code using postman, it uploads just one file and saves only that file to the database. 问题是,当我使用邮递员测试代码时,它仅上载一个文件并将该文件仅保存到数据库。 (I'm using multifileupload[] as the key and file type to upload two or more files with one input.) (我使用multifileupload[]作为键和文件类型来通过一个输入上传两个或多个文件。)

My code is below: 我的代码如下:

$app->post('/uploadfile',function(Request $request, Response $response, array $args) {
  $decodedsenttoke = $request->getAttribute('decoded_token_data');
  $directory = $this->get('upload_directory');
  $uploadedFiles = $request->getUploadedFiles();

  foreach ($uploadedFiles['multifileupload'] as $uploadedFile) {
    if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
      $pathOfUploadedFiles = "http://someaddress.com/uploads/";
      $filename = moveUploadedFile($directory, $uploadedFile);
      $pathOfUploadedFiles .= $filename;
      $input = $request->getParsedBody();
      $insertsql = "INSERT INTO files (picturelink   ,  picturetitle ,  appointid)"
        ."VALUES (:picturelink  , :picturetitle , :appointid )";
      $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
      $sth = $this->db->prepare($insertsql);
      $sth->bindParam("picturelink", $pathOfUploadedFiles);
      $sth->bindParam("picturetitle", $input['picturetitle']);
      $sth->bindParam("appointid", $input['appointid']);
      $sth->execute();
      $insertArray = array('message'=>'inserted');

      return $this->response->withJson($insertArray);
    }
  }
});

You have 你有

return $this->response->withJson($insertArray);

at the end of the inside of the if inside the loop - this will automatically exit the code and return the content after the first file has uploaded. if循环内部的末尾-这将自动退出代码并在第一个文件上传后返回内容。

Move this to the end when all files have been uploaded. 上传完所有文件后,将其移至末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM