0

I want to store image in database directly without storing it in a folder on my explorer. If possible than how? I am using php with mysql database to store image and i have define datatype LONGBLOB to store image in database. Below is my code:

   <?php

$conn = mysqli_connect("localhost","root","")or die(mysqli_error($conn));
mysqli_select_db($conn,'image') or die(mysqli_error($conn));

if(isset($_REQUEST['submit'])){
$old = $_FILES['img']['tmp_name'];
$new = $_FILES['img']['name'];

move_uploaded_file($old,$new);

echo $sql = "INSERT INTO img(`img_name`) VALUES('$new')";
$exe = mysqli_query($conn,$sql);
if($exe){
    echo "Image Uploaded Successfully....";

    }
}

echo $query = "Select `img_name` from `img`";
$ex = mysqli_query($conn,$query);

?>

<html>
    <body>
        <form method="post" name="myForm" id="myForm" enctype="multipart/form-data">
            <div>
                <input type="file" name="img"/>
            </div>
            <div>
                <input type="submit" name="submit" value="Upload"/>
            </div>
            <?php
                while($res = mysqli_fetch_array($ex)){

            ?>
            <div>
                <img height="50" width="50" src="<?php echo $res['img_name'];?>"/><br/>
            </div>
            <?php } ?>
        </form>
    </body>
</html>
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Bhatt Akshay
  • 159
  • 1
  • 14
  • Did you look up "PHP store image in database" on Google? Where did you get stuck? – David Mar 04 '16 at 12:17
  • It is, though it generally isn't recommended; store the file in the server filesystem, and just store a filepath in the database – Mark Baker Mar 04 '16 at 12:18
  • It show me to store image in folder on my explorer and save the name only in a database, otherwise store the image on my explorer and store full image path on database. – Bhatt Akshay Mar 04 '16 at 12:20
  • @Mark yes as i mentioned it in comment i have refer that, but my requirement is mentioned as above in my question. – Bhatt Akshay Mar 04 '16 at 12:22
  • You are confusing three entirely different concepts: URL, disk path and, well, data. – Álvaro González Mar 04 '16 at 12:32
  • I am not confusing, read my question properly i don't want to use move_uploaded_file() which store image in my folder on my explorer. I want to store it directly in database using LONGBLOB datatype @Alvaro Gonzalez – Bhatt Akshay Mar 04 '16 at 12:39
  • You *are* confusing, read your *code* properly. – Álvaro González Mar 04 '16 at 12:41
  • @BhattAkshay Why? You'd lose out on extensions and if done incorrectly you'll be fetching *megabytes* if not *gigabytes* from your database whenever you `SELECT`. Why would you want all the file data to be sent over the MySQL socket rather than PHP just reading from the filesystem? Let's also hope that you never have to do a backup/restore of the database if you save all your data in it. – h2ooooooo Mar 04 '16 at 12:43
  • I need it as per requirement in my project to stick it directly in database. – Bhatt Akshay Mar 04 '16 at 12:46
  • 1
    @BhattAkshay - then you really should be turning round and telling the person who specified this requirement that it's a really inefficient compared with using the filesystem - [take a look at the answers here](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay).... and shouldn't be used unless they have a very specific technical or business reason to use the database instead of the filesystem – Mark Baker Mar 04 '16 at 13:24

2 Answers2

1

You would need to convert the image into base64. something like this:

$path = $_FILES['img']['tmp_name'];
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

It'd be better to save it as a string rather than BLOB if possible. If it has to be a BLOB you'd have to convert the string and then convert it back when you're retrieving from the database. If you store it as a string, the image will even work with the base64 as the src of the image.

Hazonko
  • 1,025
  • 1
  • 15
  • 30
  • I need my image in database directly don't want to store it in my folder in explorer. $path = 'myfolder/myimage.png'; I don't want this. – Bhatt Akshay Mar 04 '16 at 12:25
  • I've edited my response to work with your example. Apologies, i thought it obvious to just swap it out. – Hazonko Mar 04 '16 at 12:30
  • BLOB Columns can store binary data; while less efficeinet, this is a method to embed the binary image data directly in the markup – Mark Baker Mar 04 '16 at 12:55
  • you could also embed base64 image data straight into the markup, rather than a BLOB, which is why i suggested using it. – Hazonko Mar 04 '16 at 13:11
0
            move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
            $instr = fopen("latest.img","rb");
            $image = addslashes(fread($instr,filesize("latest.img")));

            mysql_query ("insert into pix (mgdata) values ('".image."')");

And after delete uploaded image, image will be in DB

After you can send image from DB to browser

    $gotten = @mysql_query("select * from pix order by pid desc limit 1");
    if ($row = @mysql_fetch_assoc($gotten)) {
    $title = htmlspecialchars($row[title]);
    $bytes = $row[imgdata];
    } 

    // If this is the image request, send out the image

    if ($_REQUEST[gim] == 1) {
    header("Content-type: image/jpeg");
    print $bytes;
    exit ();
    }
    ?>
Michael
  • 1,089
  • 1
  • 11
  • 28
  • Please don't recommend the `mysql_` functions. They've been deprecated for **years** and have been completely removed from PHP 7. – h2ooooooo Mar 04 '16 at 12:45