Uploading form data from Angular 2 to Slim PHP API
I faced the same issues in Angular and Slim API and Now its working Perfectly make theses things
1- Do not send any header in your request from angular code
2- For uploaded photos You will get the uploading image in Slim app In $files array This is an example to upload image from angular to Slim API
In your component.ts
uploadimage(){var formData = new FormData();formData.append("image",this.image );return this.http.post('http://Yourserver.com/UploadeFileAPI',formData).map(response =>response.json()).subscribe(result=>{console.log("image uploaded");},error=>{console.log(error);})}
in Your Slim app
$app->post('/uploadphoto',function ($req,$res){$topic_name=$req->getParsedBodyParam('any parm name');$files=$req->getUploadedFiles();$newimage=$files['image'];}
From Slim's perspective, it simply sets the parsed body to whatever is in $_POST
, so this means that either:
- the method isn't POST
- the content-type type isn't one of
application/x-www-form-urlencoded
ormultipart/form-data
- PHP could not parse the sent data into an array
Looking at your code, I think we can rule out the first two, which implies that implies that the data is being sent in a format that PHP doesn't understand.
It looks like you're trying to send multiple files, so I think that maybe you need to change:
memory.append('memory_images', this.memory_images[i], this.memory_images[i].name);
to:
memory.append('memory_images[]', this.memory_images[i], this.memory_images[i].name);
(i.e. it's an array and needs the []
.)