0
PHP를 통해 formData (angularjs)를 검색하는 데 문제가 있습니다. 업로드 된 파일 속성에 어떻게 접근 할 수 있습니까? PHP 파일에서 $ myfile-> name?PHP에서 post를 통해 전송 된 formData를 가져 오는 방법은 무엇입니까?
컨트롤러, 서비스 및 지침 보이는 같은 :
app.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
console.log(file.name);
})
.error(function(){
});
}
}]);
app.controller('uploadController', ['$scope', 'fileUpload', function($scope, fileUpload){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ' + JSON.stringify(file));
var uploadUrl = 'json/file_upload';
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
형태 :
<div ng-controller="uploadController">
<input type="file" file-model="myFile"/>
<button ng-click="uploadFile()">upload me</button>
</div>
PHP 파일 : 그것은 일을 does't
$myFile = json_decode(file_get_contents('php://input'), true);
. 내 질문은 ... 내가 어떻게 파일 속성을 얻을 수 있는지, 즉. PHP 파일에서 $ myfile-> name?
'위해서 var_dump ($ _ POST)'?? – Hackerman
'.success (function (json) {console.log (json, json.data);});' –
'$ _POST' - 비어 있고, console.log (json)'이 정의되지 않았습니까? 'console.log (file.name)'는 파일 이름을 반환하지만, PHP에서는'$ file = json_decode (file_get_contents ('php : // input'), true);'가 null입니다. – ego