【发布时间】:2015-09-03 03:27:52
【问题描述】:
我使用 AFNetwork 将照片和字符串(我将 uitextview 转换为字符串)上传到我的服务器并保存在数据库中,但我不知道使用 AFNetwork 上传字符串,但我使用它来上传我的照片,它可以工作这是我的用于 viewcontroller 的 php 代码
<?php
include(“connect.php");
$uploaddir = "post_comment/original/"; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploaddir . $randomNumber . $uploadFile;
$comment_text = nl2br($_POST['comment']);
$post_no = $_POST['post_no'];
$mem_slug = $_POST['id'];
$timest = date("Y-m-d H:i:s");
echo "it is: ".$file;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}
if ($_FILES['userfile']['size']> 3000000) {
exit("Your file is too large.");
}
if(copy($_FILES['userfile']['tmp_name'],$newName)){
$sql3 = $conn->prepare("INSERT INTO `post_comment`(`comment_id`,`post_id`,`member_id`,`comment`,`image`,`status`,`timestp`) VALUES('',:postid,:member,:comment,:image,'1',:timestp)");
$sql3 ->bindValue(':postid',$post_no);
$sql3 ->bindValue(':member',$mem_slug);
$sql3 ->bindValue(':comment',$comment_text);
$sql3 ->bindValue(':image',$newName);
$sql3 ->bindValue(':timestp',$timest);
$sql3 ->execute();
}
echo "upload finished";
?>
Viewcontroller.m
NSString *comment = _commentText.text;
NSDictionary *data= @{@"post_id":urlpost,@"member_id":member_id,@"comment":self.commentText.text};
[manager POST:@"http://www.mysiteurl.com/member/comment_upload2.php"
parameters:data constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:UIImageJPEGRepresentation(self.image, 100) name:@"userfile" fileName:@"img.jpg" mimeType:@"image/jpg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"response is : %@",str);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@ *****", error);
}];
}
【问题讨论】:
-
你应该修正引号
标签: php ios objective-c iphone afnetworking