I'm having some troubles to upload files and data to my server using Angular 2 and PHP.
I've followed this File Upload In Angular 2? to upload the data and file from Angular 2 and all seems to be OK (I can see the data received by printing php://input). My Angular 2 code is something like this:
upload(data: data, file: File): Observable<any> {
let header = new Headers();
header.append('Content-Type', 'multipart/form-data');
header.append('Accept', 'application/json');
let options = new RequestOptions({ headers: header });
let formData:FormData = new FormData();
formData.append('file', file, file.name)
formData.append('data', JSON.stringify({data}));
return this.http
.post('myurl', formData, options)
.map(response => {
return response;
})
.catch(error => Observable.throw(error.message || error));
}
The problem is that the superglobals $_POST and $_FILES aren't populated, so I can't access them (and the data and file uploaded so). I have the same problem when uploading only data and I solved it manually-populating the $_POST superglobal by doing
$_POST = file_get_contents("php://input");
but here I can't do this because of the $_FILES....
I see in this post PHP - empty $_POST and $_FILES - when uploading larger files that the problem could be that post_max_size or upload_max_filesize are small, but I set them to 100M (enought for my purposes) and still the same. Even I toured memory_limit higiher but nothing.
I think that my problem have to be in my server side, could be the headers I set or some configuration I missed. I discarded any CodeIgniter issue (I'm using last version of CodeIgniter) because I tryed to post to a simple php file and I have the same problem. The simplest server side I'm used is:
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type');
exit(var_dump($_POST, $_FILES));
?>
But I get empty arrays...
Does anybody have any idea of what can I do?