Upload audio file using fileTransfer plugin, using PHP server side


#1

I am trying to upload a audio file, one is .flac and the other.wav file, to a remote server. The file is sent to the server but I get an error back from the server saying that the index of the file was not found. I have double checked that I have the correct fileKey in my ionic FileUploadOptions options but still getting the error

How can I pass the correct index so my server code recognizes the file? I tested using Postman and it works correctly when I input the key name

IONIC CODE

let options: FileUploadOptions = {
	     fileKey: 'filex',
	     fileName: 'record.flac',
	     mimeType: 'audio/flac',
	     httpMethod : 'post',
	     chunkedMode: false,
	     headers: {'Content-Type': 'multipart/form-data'}
	 }

	 this.fileTransfer.upload(this.file.externalDataDirectory+'record.flac', 'http://xxxx.us-east-2.elasticbeanstalk.com/api/upload', options)
	   .then((data) => {
	     // success
	     console.log(data);
	   }, (err) => {
	     // error
	     console.log(err);
	 })

PHP CODE

         $info = pathinfo($_FILES['filex']['name']);
         $ext = $info['extension']; 
         $newname = 'dog'.'.'.$ext;
         $target = 'uploads/audio/'.$newname;
         $moved = move_uploaded_file( $_FILES['filex']['tmp_name'], $target);

#2

Did you try to var_dump $_FILES? :slight_smile:


#3

Nog only try to dump $_FILES.
First at all check if you receive any data from the call.

Fix error handling in PHP so if there is no data send, return an JSON fault back.
As @mich356c said. Check this first. After that we can help you further.


#4

Yes I tried var_dump and it returns array(0) an empty array


#5

So probably your data is not sending the correct way.

Check if this is not empty:

this.file.externalDataDirectory+'record.flac

#6

I have checked in the /data/files folder and it is there, this.file.externalDataDirectory returns correct file path also


#7

Can you give us that information?

Do:

console.log(this.file.externalDataDirectory + ' record.flac');

And paste it here.


#8

It is: file:///storage/emulated/0/Android/data/io.ionic.starter/files/record.flac


#9

Ok, clear. it’s filled.

Now let’s check further inside your PHP.
You only checked with a var_dump around your $_FILES?

Did you checked $_POST to?


#10

same thing array(0), empty array as $_FILES


#11

Take a look at this post:


#12

This seems to be for ionic 1, not sure if I would have to use encodeURI in the fileTransfer for ionic 2…?


#13

#14

Hi wthijmen,

      I am trying Upload audio file using fileTransfer plugin. Can you please help me onthis