PHP保存损坏的图像

I uploaded an image from iOS to cakephp by coding it to base 64 and this is my code on iOS :

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    NSString *encodedString = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    modelMFront.text = encodedString;

then I get the image on cake and tried to saved :

$filename_path = 'test'.".jpg";
$decoded=base64_decode($this->request->data['model_mfront']);
file_put_contents(WWW_ROOT.$filename_path,$decoded);

the problem is that the image is damaged http://imgur.com/a/LESJ3

any idea how to fix this ?!

I fixed it . the problem was with the space when I encode the image so I had to replace the space by + in php

$filename_path = 'test'.".jpg";
$img = $this->request->data['model_mfront'];
$img = str_replace(' ','+',$img);
$decoded=base64_decode($img);
file_put_contents(WWW_ROOT.$filename_path,$decoded);