0

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>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Upload an Image</title>
    </head>
    <body>
        <form action="index.php" method="POST" enctype="multipart/form-data">
            File: 
            <input type="file" name='image'/> <input type="submit" value="submit">
        </form>

        <?php
        $check = false;
        //connect to database
        try{
            $db = new mysqli('','','','');

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

        if(isset($_FILES['image'])){
            $file = $_FILES['image']['tmp_name'];//['tmp_name'] is the temporary location 
        }

        if(!isset($file)){
            echo 'please select an image';
        }else{
            $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';
            }else{

                $insert = $db->prepare("INSERT INTO store VALUES (?, ?, ?)");
                $insert->bind_param('bsi', $image, $image_name, $image_size);
                if(!$insert->execute()){
                    echo "Problem uploading file";
                    }else{
                    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;
                }
            }
        }
        ?>
    </body>
</html>

get.php

<?php
try{
    $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';
}else{
$stmt->bind_param('s',$id);
$stmt->execute();
$stmt->bind_result($col1,$col2,$col3);
$stmt->fetch();

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

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

echo $col3;

}
2
Contributors
2
Replies
15
Views
3 Years
Discussion Span
Last Post by dag25
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.