0

I want to upload a picture from react-native to PHP Symfony server. I pick the picture with ImagePicker.showImagePicker and send it with RNFetchBlob.fetch, but in Symfony the file seems to be empty. The $file->getMimeType() return "file does not exist or is not readable" and the content type of the file is octet-stream. It Should be image/jpeg.

Any Idea ? Thanks for help :)

PHP code:

private function uploadFile(Request $request, $actualFilename = null)
{
        $file = $request->files->get('userfile');
        var_dump($file->getMimeType());
}

React-native code :

const options = {
      title: 'Select Photo',
      takePhotoButtonTitle: "Take photo title",
      chooseFromLibraryButtonTitle: "Choose a photo",
      quality: 1
    };

    ImagePicker.showImagePicker(options, response => {
        deviceStorage.getItem('jwt').then(jwt => {

          const endpoint = 'someEndpoint'

          RNFetchBlob.fetch('POST', endpoint, {
            Authorization : "Bearer " + jwt,
            otherHeader : "foo",
            'Content-Type' : 'multipart/form-data',
          }, [
            { name : 'userfile', filename : 'image.jpg', type:'image/jpeg', data: response.data}
          ]).then((resp) => {
            console.log(resp);
          }).catch((err) => {
            console.log(err);
          });
        });
    });
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97

1 Answers1

0

You have passing the image data, so in the server you get a base64, to retrieve the image you will for example:

private function uploadFile(Request $request, $actualFilename = null){
    $base64Image = $request->get('userfile');
    $data = base64_decode($base64Image);
    file_put_contents($imagePath, $data);
}

$imagePath is where you save the image.

If you use vichuploader to manage the images take a look here base64-image-to-image-file-with-symfony-and-vichuploader