簡體   English   中英

使用Angular js和php上傳多文件

[英]Multi File upload using Angular js and php

我們正在接管一個使用Angular js進行數據和多文件上傳的表單的網站(最多4個文件)。

我們正在發布到.php文件。 我已經能夠分離和捕獲現場數據-但似乎只能“看到”一個文件。 如果我上傳了多個文件,我只會看到添加到表單中的最后一個文件。

有人可以幫我使用PHP獲取文件嗎?

表單HTML(用於文件):

<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代碼-對於我所看到的文件:

$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;
    }

我們使用過的僅有的PHP代碼可以使我們得到任何回報-但同樣-僅1個,或者最后一個文件名是:

    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 />";
}

如果有人可以向我們展示PHP代碼,則需要使用它們捕獲這些上傳的文件。

謝謝

創建一個名稱數組並發布動態文件名:

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

在上傳中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM