我不知道问题出在我的PHP代码还是在objective-c方面。我不明白这一点,所以将不胜感激任何帮助获取文件从我创建到MySQL数据库的iPad应用程序上传。现在,只有文件的描述被发布到数据库中,但blob /文件不是。任何帮助将非常感激!为什么我的文件没有从iPad上传到MySQL DB?
这是我的PHP代码:
<?php
$username = "person";
$password = "xxxxxxx";
$database = "database";
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");
$file = $_FILES['file'];
$name = $file['tmp_name'];
$testpage = file_get_contents($name);
$testpage = mysql_real_escape_string($testpage);
mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");
mysql_close();
?>
这是我的目标c代码,包装成一个HTTP分组
NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[post_dict setObject:@"test_value" forKey:@"test_key"];
[post_dict setObject:[NSURL fileURLWithPath:[pdfUrl absoluteString]] forKey:@"file"];
NSData* regData = [self generateFormData:post_dict];
[post_dict release];
NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://myserver/upload.php"]];
[post addValue: @"multipart/form-data; boundary=_insert_some_boundary_here_" forHTTPHeaderField: @"Content-Type"];
[post setHTTPMethod: @"POST"];
[post setHTTPBody:regData];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
NSLog(@"%@", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
谢谢, 罗西
谢谢,我给了一个镜头 - 看到我的编辑,但仍然没有骰子...我得到的是数据库中的文件的名称。 – Rossi 2012-04-02 21:23:27
我使用他的generateFromData方法,并按照您的建议修改了我的代码。上面显示的编辑。它仍然不起作用,但我真的很感谢你的帮助!你碰巧看到其他可能错误的东西吗? – Rossi 2012-04-02 22:01:43
你严重地是我的英雄。认真对待。感谢您的帮助!我真的很感激它!它的工作原理真的很棒。 – Rossi 2012-04-04 05:05:45