4

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?

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 Answer 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");

Sign up to request clarification or add additional context in comments.

2 Comments

I checked the permission and it was read only. When I converted it into read write permission, then it started working. Thank you @Mario.]
How to check permission without using terminal . Step1: Right click on folder. Step2: Click on get info . Step3: Check the Sharing and permissions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.