使用POST将文件从iOS发送到PHP

问题描述:

我正在尝试将音频文件发送到PHP服务器。这是Objective-C代码:

I'm trying to send an audio file to a PHP server. Here is the Objective-C code:

NSData *data = [NSData dataWithContentsOfFile:filePath];

NSLog(@"File Size: %i",[data length]);

//set up request
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//required xtra info
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//body of the post
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"thefile\"; filename=\"recording\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postbody];
apiConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

以下是PHP代码:

if (is_uploaded_file($_FILES['thefile']['tmp_name'])) {
    echo "FILE NAME: ".$_FILES['thefile']['name'];
} else {
    echo "Nothing";
}

文件大小取决于录制的音频长度,但有数据。

The File Size varies depending on the length of audio being recorded, but there is data.

我收到Nothing的回复。

I'm getting a response of "Nothing".

调试:在PHP端,尝试:

To debug: on the PHP side, try:

$file = $_POST['name'];
echo $file

尝试使用以下代码段代替当前代码。将网址(www.yourWebAddress.com)和脚本名称更改为适合您问题的值。

Try the following code segment in place of your current code. Change the url (www.yourWebAddress.com) and script name to appropriate values for your problem.

NSData *data = [NSData dataWithContentsOfFile:filePath];
NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@"name=thefile&&filename=recording"];
[urlString appendFormat:@"%@", data];
NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding 
                                   allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *baseurl = @"https://www.yourWebAddress.com/yourServerScript.php"; 

NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" 
          forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:postData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
[connection start];

NSLog(@"Started!");

在服务器端,我有以下代码:

On the server end, I have the following code:

<?php

    $name  = $_POST['name'];
    $filename  = $_POST['filename'];
    echo "Name = '" . $name . "', filename = '" . $filename . "'.";

    error_log ( "Name = '" . $name . "', filename = '" . $filename . "'." );

?>

我收到以下输出:

[23-May-2012 11:56:18] Name ='thefile',filename ='recording'。

[23-May-2012 11:56:18] Name = 'thefile', filename = 'recording'.

我不知道为什么它不起作用您。你必须缺少一步。尝试在上面引用数据的url POST代码中注释掉两行,看看你的服务器端至少可以获得名称和文件名的纯文本。

I don't know why it is not working for you. You must have a missing step. Try commenting out the two lines in the url POST code above that references 'data' to see that you can get at the least the plain text for name and filename to your server end.

祝你好运!