从iOS到PHP在数据库中存储图像BLOB

时间:2021-12-29 16:58:22

I'm trying to send new picture from iOS to database using PHP.

我正在尝试使用PHP将新图片从iOS发送到数据库。

I'm getting a zero byte BLOB in the DATABASE... Any help would be greatly appreciated :)

我在数据库中得到一个零字节BLOB ...任何帮助将不胜感激:)

CODE:

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

The output I'm getting:

我得到的输出:

Response: BLOB: 

And Getting New BLOB in database

并在数据库中获取新的BLOB

[BLOB - 0B]

1 个解决方案

#1


0  

FOR POST METHOD READ MY OWN ANSWER FROM.

对于后置方法,请阅读我自己的答案。

And Use This code For image posting.

并使用此代码用于图像发布。

NSString  *imgPath = fullPathOfYourImage;
    if([[NSFileManager defaultManager] fileExistsAtPath:imgPath])
    {
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];                                                        
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"YourImageName.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                                                                                                               ^..YourImageName….^

        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithContentsOfFile:imgPath]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }

#1


0  

FOR POST METHOD READ MY OWN ANSWER FROM.

对于后置方法,请阅读我自己的答案。

And Use This code For image posting.

并使用此代码用于图像发布。

NSString  *imgPath = fullPathOfYourImage;
    if([[NSFileManager defaultManager] fileExistsAtPath:imgPath])
    {
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];                                                        
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"YourImageName.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                                                                                                               ^..YourImageName….^

        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithContentsOfFile:imgPath]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }