在php上上传多个图像

时间:2018-09-11 03:10:40

标签: php

我正在使用拖放区和angularjs。我想上传多个图像并将其发送到服务器。我正在尝试按自定义方式处理上传,而不是在拖放区域中处理。我将所有接受的文件存储在ng-model的拖放区中,以便获取所有图像并上传到服务器。我的问题是如何在php上循环数组图像。知道如何实现吗?

js

 $scope.saveform = function(){
  var fd=new FormData();
  console.log($scope.info.allimages);

  fd.append('type','form');
  fd.append('file',$scope.info.allimages);

    $http.post(httprequest, fd,{
          transformRequest: angular.identity,
          headers: {'Content-Type': undefined, 'Process-Data': false}
      }
  ).then(function(response) {


  });
}

js console.log结果:

[File {upload=Object, status="queued", ...}, File {upload=Object, status="queued", ...}]

dropzone配置:

  $scope.dropzoneConfig2 = {
    'options': {
        url : httprequest,
        acceptedFiles : 'image/jpeg, images/jpg, image/png',
        addRemoveLinks : true,
        parallelUploads : 20,
        maxFiles: 20,
        uploadMultiple: true,
        autoProcessQueue : false,
        init: function () {
              var vm = this;
        }
    },
    'eventHandlers': {
          'addedfile': function(file) {
              $scope.info.allimages = this.getAcceptedFiles();
          },
          'removedfile': function(file) {
          },
          'sending': function (file, xhr, formData) {
          },
          'success': function (file, response) {
          }
     }

  }

php:

if($_POST['type']=='form'){
echo json_encode($_POST['file']));}

php结果:

"[object File],[object File]"

1 个答案:

答案 0 :(得分:1)

尝试这样。

for($count = 0; $count<count($_FILES["files"]["name"]); $count++)
{
    $_FILES["file"]["name"] = $_FILES["files"]["name"][$count];
    $_FILES["file"]["type"] = $_FILES["files"]["type"][$count];
    $_FILES["file"]["tmp_name"] = $_FILES["files"]["tmp_name"][$count];
    $_FILES["file"]["error"] = $_FILES["files"]["error"][$count];
    $_FILES["file"]["size"] = $_FILES["files"]["size"][$count];
}