uploadFile does not posts image to server file (PHP)

Viewed 281

In my app I have the cordova camera plugin which allows me to take a picture and I can get the URI from it.

const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.FILE_URI,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
};
this.camera.getPicture(options).then(
  imageURI => {

  },
  err => {

  }
);

I then use that URI to upload to my server.

this.http
    .uploadFile(
        'https://example.com/pages/attachments.php, {
            action: 'INSERT'
        }, {},
        'file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1583144250046.jpg',
        'picture'
    )
    .then(
        fileResult => {
            console.log(fileResult);
        },
        err => {

        }
    );

But I am getting these errors from the console.log result:

Undefined index: file in on line 1235

Undefined index: file in on line 1236

These are the lines in attachments.php:

// The tmp_name is the name of the temporary file on the server.
$tmp_name = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
    

It seems like that $_FILES is empty but i'm not sure why. I assumed using http.uploadFile would send the file but i'm not sure why it isn't. Any help?

2 Answers

Seems to be a problem related to your PHP code. Check this document, I think that file is wrong. Maybe you should try picture instead, because you used this name while uploading the file? But I'm not an PHP expert.

According to your JS code you should use $_FILES['picture']

Are you using a PHP framework? I would advice you to do so in order to have your file upload handling more robust and more secure.

Related