I am trying to upload a recorded .caf file to my web server. The file is there, because it plays after recording, and my play configurations are correct as well, because I can play a regular .caf file from my server in my app. However when I upload the caf, the php script returns a "SUCCESS", and a file is created that is "supposed" to be the uploaded .caf file, but the file is either blank, or 2 bytes in size. I know the file isn't right because when I download it and place the file into an audio editing software, it is blank. I have the following OBJECTIVE-C code:
NSString *file2 = [[NSBundle mainBundle] pathForResource:filePath ofType:@"caf"];
NSData *file1Data = [[NSData alloc] initWithContentsOfFile:file2];
NSString *urlString = @"http://sayroom.com/SR_Mobile_App/scripts/upload_audio.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"thefile.caf\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:file1Data]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Return String= %@",returnString);
NSLog(@"Audio Sent Successfully");
Here is my php code:
$uploaddir = 'audio/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if(!$_FILES['userfile']['tmp_name']){
echo "no file";
exit();
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo $_FILES['userfile']['tmp_name']."done";
} else {
echo "ERROR";
}
I have been deadling with this issue all day, and i cannot find any help on it online. I have tried numerous approaches, and I haven't seen any progress yet.
Any ideas?