我们正在接管使用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 Code - 我看到的文件:
$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代码。
由于
答案 0 :(得分:0)
创建名称数组并发布动态文件名:
var names = []
foreach file:
names.push(files[i].name)
上传:
method: 'POST',
data: $scope.model,
file: $scope.attachments,
fileFormDataName: names