0

I am new to php & mysql I was trying to to save user image to the MySql datbase. I have seen this solution on php.net

<?php
    $data = file_get_contents('path/to/image.jpg');
    $data = base64_encode($data);

    //after reading data would be like this
    $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';

    $data = base64_decode($data);

    $im = imagecreatefromstring($data);
    if ($im !== false) {
        header('Content-Type: image/png');
        imagepng($im);
        imagedestroy($im);
    }
    else {
        echo 'An error occurred.';
    }
?>

it works but i am not getting how to store value of '$data' in MySql database. Should I take varchar datatype for my field and what would be the maximum limit of varchar??

Is this correct way or there are better ways then this if yes then what are they??

3
  • 1
    Why bloat your table with image data when you can just store the image in the folder? It's faster and less processing when it comes time to retrieve the image. Commented Oct 4, 2013 at 12:46
  • People mostly save only link to image in database, its quite faster... Commented Oct 4, 2013 at 12:47
  • @j08691: you are right but it shows the directory structure on html so i am trying this. Commented Oct 4, 2013 at 12:48

1 Answer 1

1

You could use text. Then you don't need to define maximum length. since the base64 encoded string could be quite long i its a big image.

An other solution would be to store the image path instead.

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

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.