php – 从iOS应用程序将图像存储到MYSQL数据库中

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 从iOS应用程序将图像存储到MYSQL数据库中脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用MysqL数据库从我的iOS应用程序中存储图像.我使用base64encoding和解码技来存储图像.
问题:
它将名称存储在数据库中作为字符串即可.但它不是将图像存储在htdocs文件夹中.我的问题在哪里以及如何解决

PHP代码

<?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:&amp;response error:&error];

解决方法

首先检查要保存的目录是否具有写入权限.

然后尝试:imagejpeg($img,$_SERVER [‘DOCUMENT_ROOT’].“/ image_name.jpg”);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 从iOS应用程序将图像存储到MYSQL数据库中全部内容,希望文章能够帮你解决php – 从iOS应用程序将图像存储到MYSQL数据库中所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。