title: Storing image into MYSQL database from iOS application
body: <p>I am using <code>MYSQL</code> database to store image into it from my <code>iOS</code> app. I am using <code>base64encoding</code> and decoding technique to store image.
  <strong>Problem:</strong>
  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?</p>
  
  <p><strong>php code:</strong></p>
  
  <pre><code>&lt;?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();
          }
         }
  
  ?&gt;
  </code></pre>
  
  <p><strong>Client side code:</strong></p>
  
  <pre><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:&amp;response error:&amp;error];
  </code></pre>