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

时间:2021-09-19 16:56:50

I am using MYSQL database to store image into it from my iOS app. I am using base64encoding and decoding technique to store image. Problem: It is storing the name in database as a string which is OK. But it is not storing the image in htdocs folder. Where is my problem and how to solve it?

我正在使用MYSQL数据库从我的iOS应用程序中存储图像。我使用base64encoding和解码技术来存储图像。问题:它将名称存储在数据库中作为字符串即可。但它不是将图像存储在htdocs文件夹中。我的问题在哪里以及如何解决?

php code:

<?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();
        }
       }

?>

Client side code:

客户端代码:

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];

1 个解决方案

#1


1  

First check if the directory you want to save in has writing permissions.

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

Then try: imagejpeg($img, $_SERVER['DOCUMENT_ROOT'] . "/image_name.jpg");

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

#1


1  

First check if the directory you want to save in has writing permissions.

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

Then try: imagejpeg($img, $_SERVER['DOCUMENT_ROOT'] . "/image_name.jpg");

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