i am trying to Upload and Store an Image inside a MySQL, for some reason i got a broken image when photo is submitted .
i know its a bad idea ti store images in using database i am doing this for testing purposes.
please help

<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Upload an Image</title>
        <form action="index.php" method="POST" enctype="multipart/form-data">
            <input type="file" name='image'/> <input type="submit" value="submit">

        $check = false;
        //connect to database
            $db = new mysqli('','','','');

        }catch(Exception $e){
            echo $e->getMessage();

            $file = $_FILES['image']['tmp_name'];//['tmp_name'] is the temporary location 

            echo 'please select an image';
            $image = file_get_contents($_FILES['image']['tmp_name']);
            $image_name = $_FILES['image']['name'];
            $image_size = getimagesize($_FILES['image']['tmp_name']);

            if($image_size == false){
                echo 'thats not an image';

                $insert = $db->prepare("INSERT INTO store VALUES (?, ?, ?)");
                $insert->bind_param('bsi', $image, $image_name, $image_size);
                    echo "Problem uploading file";
                    printf("%d Row inserted.\n", $insert->affected_rows);
                    echo $lastid = $db->insert_id.'<br>';//returns the last id that was inserted 
                    echo 'image uploaded.<p/> Your Image:<p/><img src=get.php?id=$lastid>';
                    $check = true;


    $db = new mysqli('','','','');
}catch(Exception $e){
    echo $e->getMessage();

$id = $_REQUEST['id'];

if(!$stmt = $db->prepare("SELECT * FROM store WHERE id = ?")){
    echo 'probelm with db';

//echo $col1.''.$col2;

header('Content-type: image/jpg');

echo $col3;


Recommended Answers

All 2 Replies

You have to use base64_encode
There is even an example there by Cristiano Calligaro

this didnt help
$image = base64_encode(file_get_contents($_FILES['image']['tmp_name']));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.