I think my question is clear enough but here some details. I pretty new to Angular and PHP so I probably did a lot of mistakes or missing something important :).
我认为我的问题很清楚,但这里有一些细节。我对Angular和PHP很新,所以我可能犯了很多错误或遗漏了一些重要的东西:)。
I have first of all a form containing text and file inputs. Like this :
我首先拥有一个包含文本和文件输入的表单。像这样 :
<div><label for="textInput">Text input</label></div>
<div><input id="textInput" name="textInput" type="text" ng-model="form.textInput"></div><br/>
<div>Put some file please</div>
<div>
<input id="file1" type="file" name="file1" ng-model="form.file1"><br/>
<input id="file2" type="file" name="file2" ng-model="form.file2">
</div>
To post file with ng-model I used this directive :
要使用ng-model发布文件,我使用了这个指令:
(function () {
fileInput.$inject = [];
function fileInput() {
var fileTypeRegex = /^file$/i;
return {
restrict: 'E',
require: '?ngModel',
link: link
};
function link(scope, element, attrs, ngModel) {
if (ngModel && element[0].tagName === 'INPUT' && fileTypeRegex.test(attrs['type'])) {
element.on('change', function () {
var input = this;
if ('multiple' in attrs) {
var files = Array.prototype.map.call(input.files, function (file) { return file; });
ngModel.$setViewValue(files);
}
else {
ngModel.$setViewValue(input.files[0]);
}
});
}
}
}
angular.module('ng-file-input', []).directive('input', fileInput);
}());
Finally, I send data with $http :
最后,我用$ http发送数据:
$http({
url: window.API_URL + 'postulate.php',
method:'POST',
data:$scope.form
}).then(function successCallback(response){
console.log(response.data);
console.log($scope.form);
});
In PHP file I only get $_POST data and I print it :
在PHP文件中我只获得$ _POST数据并打印出来:
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);
echo json_encode($_POST);
And here's the problem. I get with console.log() :
这就是问题所在。我得到console.log():
Object {textInput: "the content", file1: Array[0], file2: Array[0]}
Object {textInput: "the content", file1: File, file2: File}
Did a lot of Google and tests but I can't get it to work.
做了很多谷歌和测试,但我无法让它工作。
Ask me if you want more details.
问我是否想要更多细节。
1 个解决方案
#1
2
To get the files set trough ajax from PHP you need to use the $_FILES
php global variable which actually is an array containing some field like name
, type
, tmp_name
. So if you send multiple files, the combined array should look something like this:
要通过PHP中的ajax设置文件,您需要使用$ _FILES php全局变量,该变量实际上是一个包含名称,类型,tmp_name等字段的数组。因此,如果您发送多个文件,组合数组应如下所示:
Array
(
[image] => Array
(
[name] => MyFile1.jpg (comes from the browser, so treat as tainted)
[type] => text/plain (not sure where it gets this from - assume the browser, so treat as tainted)
[tmp_name] => /tmp/php/php1h4j1o // this is were the temporally file is saved on the server
[error] => UPLOAD_ERR_OK (= 0)
[size] => 123 (the size in bytes)
)
[image] => Array
(
[name] => MyFile2.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)
)
To obtain the files in php you can check if the $_FILES is set or it is not empty:
要获取php中的文件,您可以检查是否设置了$ _FILES或它是否为空:
if (isset($_FILES['image']['name']) && (file_exists($_FILES['image']['tmp_name']))) {
// do the job here
}
Here the name of the image
field depends on how you defined the form in your html. For example if you have an input field declared as:
这里图像字段的名称取决于您在html中定义表单的方式。例如,如果您将输入字段声明为:
<input type="file" name="image" id="image">
then the name here is how you identify the $_FILES
first field. So actually you get the files based on the array key.
那么这里的名称是你如何识别$ _FILES第一个字段。实际上你实际上是根据数组键获取文件。
Hope that's clear.
希望很清楚。
#1
2
To get the files set trough ajax from PHP you need to use the $_FILES
php global variable which actually is an array containing some field like name
, type
, tmp_name
. So if you send multiple files, the combined array should look something like this:
要通过PHP中的ajax设置文件,您需要使用$ _FILES php全局变量,该变量实际上是一个包含名称,类型,tmp_name等字段的数组。因此,如果您发送多个文件,组合数组应如下所示:
Array
(
[image] => Array
(
[name] => MyFile1.jpg (comes from the browser, so treat as tainted)
[type] => text/plain (not sure where it gets this from - assume the browser, so treat as tainted)
[tmp_name] => /tmp/php/php1h4j1o // this is were the temporally file is saved on the server
[error] => UPLOAD_ERR_OK (= 0)
[size] => 123 (the size in bytes)
)
[image] => Array
(
[name] => MyFile2.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)
)
To obtain the files in php you can check if the $_FILES is set or it is not empty:
要获取php中的文件,您可以检查是否设置了$ _FILES或它是否为空:
if (isset($_FILES['image']['name']) && (file_exists($_FILES['image']['tmp_name']))) {
// do the job here
}
Here the name of the image
field depends on how you defined the form in your html. For example if you have an input field declared as:
这里图像字段的名称取决于您在html中定义表单的方式。例如,如果您将输入字段声明为:
<input type="file" name="image" id="image">
then the name here is how you identify the $_FILES
first field. So actually you get the files based on the array key.
那么这里的名称是你如何识别$ _FILES第一个字段。实际上你实际上是根据数组键获取文件。
Hope that's clear.
希望很清楚。