0

I am having issues storing a .JPG image in my MySQL database. I am using the PHP code below to store it into the database (in long blob form). My code appears to be completely error free and every thing seems to act as it should aside from the fact that the image is only stored in what appears to be 36 B size on PHPMyAdmin.

I would expect the image to be much larger (22 MB supposedly).

<?php
require '../FirstWebsite/CommonFunctions.php';
DB_Connect();
$dblink = DB_Connect();
?>
<html>
<body>
    <form method="post" enctype="multipart/form-data">
        <br/>
        <input type="file" name="selectImage">
        <br/>
        <input type="submit" name="submitImage" value="submit">
    </form>
    <?php
    if(isset($_POST['submitImage'])){
        if(getimagesize($_FILES['selectImage']['tmp_name'])==FALSE){
            echo 'please select an actual image';
        }
        else{
            $getImage = mysqli_real_escape_string($dblink,                        $_FILES['selectImage']['tmp_name']);
            $name = mysqli_real_escape_string($dblink, $_FILES['selectImage']['name']);
            $image = base64_encode($getImage);
            saveImage($name, $image, $dblink);
        }
    }
    function saveImage($name, $image, $dblink){
        $query = "INSERT INTO trialtable2 (caption, actualpicture) VALUES       ('$name','$image')";
        $queryCheck = mysqli_query($dblink, $query);
        if($queryCheck == TRUE){
            echo '<br/> uploaded';
        }
        else{
            echo '<br/> not uploaded';
        }
    }
    ?>
</body>
</html>

what I have done

  • attempted uploading .jpeg .JPG .jpg (same but still...) .tif .png

  • file_upload = on (or whatever) is already on in php.ini

  • it is a long blob type in the data base table

Supposedly storing images in a database is no the way to go however this is what I am working with for the time being.

versions

  • wamp: 2.5
  • apache: 2.4.9
  • mysql: 5.6.17
  • php: 5.5.12

I believe that this question is not a replica as I haven't been able to find an answer elsewhere. If I am doing something incorrectly feel free to let me know as I am still new to the community.

enter image description here

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • This likely isn't your main problem, but do not escape the image before passing it to base64_encode -- there's no need, and it may corrupt your image. – Martin Dec 10 '15 at 07:14
  • @Martin I had actually already tried that, using no string escaping, and also using the old school addslahes() method. All that happened is the BLOB became 34 B or 32 B. Should I try again? – Solomon Broadbent Dec 10 '15 at 07:16

1 Answers1

3

tmp_name is the temporary path to the file, this is why you're only seeing a few bytes.

  • Remove any escaping (including addslashes).
  • use file_get_contents($_FILES['selectImage']['tmp_name']) to get the actual contents of the image for inserting into your database.

It generally isn't a great idea to store the blobs in the database, and better to store the path (like you're currently doing). If you choose to go the path route, you need to use something like move_uploaded_file to move the file to a persistent location.

Community
  • 1
  • 1
Martin
  • 6,632
  • 4
  • 25
  • 28
  • Thank you, will try compiling this. I would rather store it as a file path as I have succeeded in this before. However, I first need to learn how persistence works when I launch the page. This is just something temporary for the time being. – Solomon Broadbent Dec 10 '15 at 07:26
  • no problem, let me know if you have any follow-up questions I can clarify – Martin Dec 10 '15 at 07:29
  • upvote for the moment. have to go for a while before I tick you. I wont forget. Apparently that's bad. Weirdly it is now storing images bigger then they are on my computer. – Solomon Broadbent Dec 10 '15 at 07:32
  • base64 encoding will give you ~33% bloat. There's many less characters than the binary equivalent ;) – Martin Dec 10 '15 at 07:33