2

I'm trying to insert a base64 string into blob by using the code below, I'm getting a blob added to the database but it's a damaged file with no extension.

Background:

[request setHTTPMethod:@"POST"];
NSString *encodedString = [binaryData base64Encoding];
NSString *bodyString = [NSString stringWithFormat:@"image=%@", encodedString];

PHP:

$json_obj = $_POST['image'];
$encodedData = str_replace(' ','+',$json_obj);
$encodedData= chunk_split(base64_encode(file_get_contents($encodedData)));
$blob = $encodedData;
$dbHandle = mysql_connect("--------","-------","------");
$dbFound = mysql_select_db("----------");

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 '<img src="data:image/jpeg;base64,' . $blob . '" />';
}
else{
      print "No Connection";
}

2 Answers 2

4

Try to use:

$encodedData= chunk_split(base64_encode(file_get_contents($json_obj)));

Then change echo "BLOB: " . "---" . $blob; to:

echo '<img src="data:image/jpeg;base64,' . $blob. '" />';

Otherwise you are just dumping the string value that's why you get this result.

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

6 Comments

I'm not tryna output it or anything, I'm just getting a damaged file with no extension in the database
It still displays the image file perfectly but having a damaged file with no extension in mysql
Changed my question with the code provided so you can see it more clearly
@Dave Try $_FILES["image"] instead of $_POST["image"]. And remove $encodedData = str_replace(' ','+',$json_obj);.
That gives me <img src="data:image/jpeg;base64,DQo=" />. With a damaged file much smaller in size in the database, I'll add a bit of the background going to php to my question
|
1

Have you tried using mysql base64 function: TO_BASE64('string'); ?

Comments

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.