我正在尝试将记录的 .caf 文件上传到我的网络服务器。该文件在那里,因为它在录制后播放,而且我的播放配置也是正确的,因为我可以在我的应用程序中从我的服务器播放常规的 .caf 文件。但是,当我上传 caf 时,php 脚本返回“SUCCESS”,并创建了一个“假定”为上传的 .caf 文件的文件,但该文件为空白或 2 个字节大小。我知道文件不正确,因为当我下载它并将文件放入音频编辑软件时,它是空白的。我有以下 OBJECTIVE-C 代码:
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");
这是我的php代码:
$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";
}
我整天都在处理这个问题,我在网上找不到任何帮助。我尝试了很多方法,但我还没有看到任何进展。
有任何想法吗?