iOS
PHPを使用してデータベースから新しい画像を送信しようとしています。
私はゼロバイトのBLOBを取得していDATABASE
ます...どんな助けも大歓迎です:)
コード:
NSString *url = @"-----------------";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
NSData *binaryData = UIImageJPEGRepresentation(image, 1.0);
NSString *encodedString = [binaryData base64Encoding];
//NSLog(@"Encoded : %@ ",encodedString);
//NSData* binaryData = UIImagePNGRepresentation(image);
NSString *bodyString = [NSString stringWithFormat:@"image=%@",encodedString];
[request setValue:[NSString stringWithFormat:@"%d", [bodyString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[bodyString dataUsingEncoding:NSASCIIStringEncoding]];//or set the type of encoding agreed with your webservice
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString;
if ( responseData && !error){
responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",responseString);
}
PHP:
$json_obj = json_decode($_POST['image']);
$blob = base64_decode($json_obj);
$dbHandle = mysql_connect("MYCONNECTION");
$dbFound = mysql_select_db("MYCONNECTION");
if($dbFound){
$check = "INSERT INTO `Images`(`imageId`, `image`, `userId`, `dateCreated`) ".
"VALUES ".
"('','$blob','0',null)";
$retval = mysql_query( $check, $dbHandle );
if(!$retval)
{
die('Could not enter data: ' . mysql_error());
}
echo "BLOB: " . $blob;
}
else{
print "No Connection";
}
mysql_close($dbHandle);
?>
私が得ている出力:
Response: BLOB:
データベースで新しい BLOB を取得する
[BLOB - 0B]