ios php mysql实例_php – 从iOS应用程序将图像存储到MYSQL数据库中

我正在使用MYSQL数据库从我的iOS应用程序中存储图像.我使用base64encoding和解码技术来存储图像.

问题:

它将名称存储在数据库中作为字符串即可.但它不是将图像存储在htdocs文件夹中.我的问题在哪里以及如何解决?

php代码:

$host='localhost';

$name='root';

$pwd='';

$db='i';

$conn=mysql_connect($host,$name,$pwd);

mysql_select_db($db,$conn);

if($conn)

{ $image=$_POST['image_string'];

if($image!='')

{

$img = @imagecreatefromstring(base64_decode($image));

if($img != false)

{

imagejpeg($img, "htdocs/".$image."");}

}

$qur=mysql_query("INSERT INTO `j` (`id`, `name`) VALUES (NULL, '$image')");

if($qur)

{

echo "inserted";

}

else

{

echo mysql_error();

}

}

?>

客户端代码:

NSData *imageData=UIImagePNGRepresentation(_imageView.image);

NSString *string;

if([imageData respondsToSelector:@selector(base64EncodedStringWithOptions:)])

{

NSLog(@"iOS 7+");

string=[imageData base64EncodedStringWithOptions:kNilOptions];

}

else

{

string=[imageData base64Encoding];

}

NSString *post=[[NSString alloc]initWithFormat:@"image_string=%@",string];

post = [post stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

NSURL *url=[[NSURL alloc]initWithString:@"http://localhost/lastImage.php"];

NSData *postDAta=[post dataUsingEncoding:NSUTF8StringEncoding];

NSString *postlength=[NSString stringWithFormat:@"%lu",(unsigned long)[postDAta length]];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];

[request setURL:url];

[request setHTTPMethod:@"POST"];

[request setValue:postlength forHTTPHeaderField:@"Content-length"];

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postDAta];

NSError *error = [[NSError alloc] init];

NSHTTPURLResponse *response = nil;

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值