简体   繁体   中英

Multi File upload using Angular js and php

We are taking over a site that is using Angular js for a form for data and multi-file upload (up to 4 files).

We are posting to a .php file. I have been able to separate and capture the field data - but can only seem to 'see' one file. If I upload multiple files I 'see' only the last one added to the form.

Can someone please help me to get the files using PHP?

Form HTML (for files):

<ul class="attachments">
<li ng-repeat="attachment in attachments" class="attachment">
    {{attachment.name}}
<a href="" ng-click="removeAttachment(attachment)"><i class="btn-submit-att-x-hover-off"></i></a>
</li>
</ul>
    <button type="button" class="attachments-button btn btn-default" ngf-select ngf-change="onFileSelect($files)" ngf-multiple="true" ng-if="attachments.length < 4">
    <i class="btn-submit-attch"></i>Upload Attachments
    </button>

Angular JS Code - for the files as I see:

$scope.attachments = [];

//onFileSelect adds file attachments to the $scope up to the
//configured maximum value.
    $scope.onFileSelect = function ($files) {
    var MAX_ATTACHMENTS = 4;
    for(var i=0; i < $files.length; i++){
        if($scope.attachments.length >= MAX_ATTACHMENTS){
        break;
        }
    $scope.attachments.push($files[i]);
        }            
    };

    $scope.removeAttachment = function (attachment) {
        var index = $scope.attachments.indexOf(attachment);
        if (index > -1) {
        $scope.attachments.splice(index, 1);
        }
    };

//POST form data
    $scope.upload = Upload.upload({
        url: 'forms/pressrelease.html',
        method: 'POST',
        data: $scope.model,
        file: $scope.attachments,
        fileFormDataName: "files"
    })

    .success(function (data, status, headers, config) {
        alertSuccess("Your press release has been submitted successfully!");
        $scope.showSuccess = true;
    }

The only PHP code we've used that has given us anything back - but again - only 1, or the last file name is:

    if(isset($_FILES['files'])){ 
$files = $_FILES['files'];

$sentfilename = $_FILES['files']['name'];
$email_body .= "I see files: $sentfilename<br />";

$tmpsentfile = $_FILES['files']['tmp_name'];

$i=0;

$getfile = $files[0];
$sentfilename = $getfile;
$sentfilesize = $_FILES['files']['size'];
$errormessage = $_FILES['files']['error'];

$email_body .= "I see files: $sentfilename<br />";
}

If anyone one can show us the PHP code we need to use to capture these uploaded files.

Thanks

Create a names array and post the dynamic file names:

var names = []
foreach file:
    names.push(files[i].name)

in upload:

  method: 'POST',
    data: $scope.model,
    file: $scope.attachments,
    fileFormDataName: names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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