2016-07-06 82 views
1

我想我的问题已经够清楚了,但在这里有一些细节。我对Angular和PHP非常陌生,所以我可能犯了很多错误或错过了重要的东西:)。

我首先有一个包含文本和文件输入的表单。就像这样:

<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>

要与NG-模型发布的文件我用这个指令:

(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); 
}()); 

最后,我用$ 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); 
}); 

在PHP文件中,我只获取$ _POST数据并打印出来:

$rest_json = file_get_contents("php://input"); 
$_POST = json_decode($rest_json, true); 
echo json_encode($_POST); 

这就是问题所在。我得到与console.log():

Object {textInput: "the content", file1: Array[0], file2: Array[0]} 
Object {textInput: "the content", file1: File, file2: File} 

做了很多谷歌和测试,但我无法得到它的工作。

询问我是否需要更多详细信息。

回答

2

要想从PHP,你需要使用PHP $_FILES全局变量实际上是包含像nametypetmp_name一些字段的数组设置槽AJAX的文件。所以,如果你发送多个文件,合并数组应该是这个样子:

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 
     ) 
) 

为了获得在PHP如果$ _FILES设为您可以检查文件或它不是空的:

if (isset($_FILES['image']['name']) && (file_exists($_FILES['image']['tmp_name']))) { 
    // do the job here 
} 

这里的image字段的名称取决于你如何在你的html中定义表单。例如,如果你有一个输入字段声明:

<input type="file" name="image" id="image"> 

那么这里的名字是你如何识别$_FILES第一场。所以实际上你会根据数组键获取文件。

希望这是明确的。

+0

我不能使用'$ _POST'?好我的坏我想我看到这个http://php.net/manual/en/features.file-upload.post-method.php所以我想我可以用'$ _POST'来做到这一点。 然后我有一些问题:你说:“要获得php中的文件,你可以检查'$ _FILES'是否被设置或它不是空的。我试图打印'$ _FILES',但我得到了console.log():'[]' –

+0

你必须在PHP中完成。做一个'echo'或'var_dump',然后'die'这样你就会收到响应。 –

+0

我确实在PHP中使用它。不是吗? console.log()的第一行对应于我的PHP响应。或者我误解了你? –

相关问题