1

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

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 :

(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({
    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 :

$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() :

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.

0

1 Answer 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:

 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:

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:

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

Hope that's clear.

Sign up to request clarification or add additional context in comments.

6 Comments

I can't use $_POST ? Ok my bad I guess i saw this php.net/manual/en/features.file-upload.post-method.php so I thought I could do it with $_POST. Then I have some questions : you said "To obtain the files in php you can check if the $_FILES is set or it is not empty". I've tried to print $_FILES but i get in console.log() : []
You have to do it in PHP. Make an echo or var_dump, then die this way you will receive the response.
I do echo it in PHP. Don't I ? The first line of console.log() corresponds to my PHP response. Or did I misunderstand you ?
And are you sure you are sending the files? If the form contains a file type normally $_FILES will capture it.
I'm not entirely sure (seems ok because console.log(form) shows files) but I don't know how to check that otherwise.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.