mysqlobjective-cdatabaseimageasiformdatarequest

Loading/Uploading image to mysql database with ASIFormDataRequest (BLOBs) for iPhone?


Hi I need to store images (user's profile picture) to mySQL DB via BLOBs, now I understand that the better way is to store images to server's file system and to save only url for them to DB,but not me the boss :) , so I'l store image to DB ! I have the php code that working nice for java script for android (using converting to binary data with base64) , I need to use it now for iPhone:this is php I need to use to insert image:

    <?php
$base     = $_REQUEST['image'];
$filename = $_REQUEST['filename'];

    //connect to the db  
$user = ‘root’;  
$pswd = '';  
$db = ‘test’;  
$conn = mysql_connect(‘localhost’, $user, $pswd);  
mysql_select_db('test'); 
$query = "INSERT INTO `test`.`photos` (`id`, `image`) VALUES (NULL,'$base')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();
echo $base;

?>

And this for get image:

<?php
$id = $_REQUEST['id'];
$username = "root";
$password = "";
$host = "localhost";
$database = "test";

mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

mysql_select_db($database) or die("Can not select the database: ".mysql_error());
//select image
$query = "SELECT * FROM  `photos` WHERE id =30 LIMIT 0 , 30";
$sql = mysql_query($query) or die(mysql_error());
while($image = mysql_fetch_array($sql)){
echo $image['image'];
}
?>

I'm trying to do something like this to store image:

        //converting image to data end to binary data (base64)
         NSData* data = UIImagePNGRepresentation(Image);
         [Base64 initialize];
         NSString *strEncoded = [Base64 encode:data];
//prepare request
         NSString *strURL = @"My URL Here.";
         ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:
    [NSURL URLWithString:strURL]];
         [request setDelegate:self];
         [request setData:strEncoded withFileName:@"image.png" andContentType:@"image/png" forKey:@"image"];

         [request startAsynchronous];

And I see in mySQL that it add new BLOB, but without my image ([BLOB-0B]). Any ideas where I lose my image data?


Solution

  • I found answer to my question: I only need to change :$base = $_REQUEST['image']; to $base = $_POST['image'];