脚本宝典收集整理的这篇文章主要介绍了php – 从iOS应用程序将图像存储到MYSQL数据库中,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<?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];
以上是脚本宝典为你收集整理的php – 从iOS应用程序将图像存储到MYSQL数据库中全部内容,希望文章能够帮你解决php – 从iOS应用程序将图像存储到MYSQL数据库中所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。