i am creating an ecommerce site and i want to add and retrieve image from database within image tag please someone help me with it...
i do not want to save the image path but whole image.....
i want to use attribute width and height with the image....

Member Avatar

Ok here's some old code I had - easier to post it than upload to a site and give you the link. It probably won't suit your needs exactly, but that's not the point - it's just meant to be an example.

You need a form something like this:

<form method="post" enctype="multipart/form-data" action="...your processing file...">
title: <input name="title" />
description: <textarea name="description"></textarea>
<input type="file" name="file" id="file"/>
<input type="submit" value = "Submit" name="submit"/>

I've left out the labels for brevity. The form can be 'sent to itself' (just leave out the action attribute), but this isn't always the best way.

Then your processing code could be something like this:


//check form is sent
    //these mime type are the only ones allowed (png, gif and jpeg files)
    $allowed_mimes = array("image/gif","image/jpeg","image/png");
    //this isn't really recommended, but cuts down code - just makes DB safe variables from $_POST superglobal array, eg.
    //$title from $_POST['title'] and $description from $_POST['description']
    extract(array_map("mysql_real_escape_string", $_POST)); 
    //OK, now check file is valid type and file actually chosen  
    if($_FILES['file']['error'] === 0 && in_array($_FILES['file']['type'],$allowed_mimes)){

    //merge and sanitize data from the uploaded file
        $file = array_map("mysql_real_escape_string",array_merge($_FILES['file'],getimagesize($_FILES['file']['tmp_name'])));
        //sanitize the actual image data
        $blob = mysql_real_escape_string(file_get_contents($_FILES['file']['tmp_name']));
        //set additional file data for SQL query
        $channels = (isset($file['channels'])) ? ", filechannels = '" . $file['channels'] . "'" : '';
        $bits = (isset($file['bits'])) ? ", filebits = '" . $file['bits'] . "'" : '';
        $mime = (isset($file['mime'])) ? ", filemime = '" . $file['mime'] . "'" : '';
        //Build SQL query
        $sql = "INSERT INTO images SET mytimestamp = " . time() . ", title = '$title', description = '$description', img = '$blob', filesize = '{$file['size']}', filename = '{$file['name']}', filetype = '{$file['type']}',filewidth = '{$file[0]}',fileheight = '{$file[1]}', filetag = '{$file[3]}', fileimgconstant = '{$file[2]}'" . $bits . $mime . $channels ;  

        //Run the query
        //Check to see if the query is successful
            echo "successfully stored";
            echo "couldn't store the image";    
        echo "no image or unsupported image format";    

Obviously this depends on your MySQL table fieldnames. In this refactored sample of mine, they are:

image_id (int)
mytimestamp (int)
img (blob)
title (varchar-25)
description (text)
filesize (int)
filewidth (int)
fileheight (int)
fileimgconstant (tinyint)
filename (varchar-50)
filetype (varchar-15)
filetag (varchar-40)
filebits (int)
filechannels (tinyint)
filemime (varchar-20)

Some of the field sizes may be overkill, so they need to be trimmed down.

You can retrieve pictures easily with a separate php file, like this - let's call it imgget.php:

    $id = intval($_GET['img']);

    $sql = "SELECT * FROM images WHERE image_id = $id";
    $r = mysql_query($sql);
        $d = mysql_fetch_assoc($r);
        header("Content-type: {$d['filemime']}");
        echo $d['img'];
        //use the default image if there's a problem - set to id = 0 and gif type
        $sql = "SELECT * FROM images WHERE image_id = 0";
        $r = mysql_query($sql);
            $d = mysql_fetch_assoc($r);
            header("Content-type: image/gif");
            echo $d['img'];

Now if you want to display an image, you can do this:

(...include db.php, DTD and other html head etc...)
<!-- CHECK an Image you know to exist PLAIN HTML -->
<p>PLAIN HTML: <img src="imgget.php?img=12" /></p>
<br />
<!-- SHOW all Images in DB -->

//get all images in DB already:
$r = mysql_query("SELECT * FROM images");
$table = "<table><thead><tr><th>Image</th><th>Data</th></thead><tbody>";
while($d = mysql_fetch_array($r)){
    //concatenate table rows
    $table .= "<tr><td><img src=\"imgget.php?img={$d['image_id']}\" /></td><td>Title: {$d['title']}<br />Description: {$d['description']}<br />Size (b): {$d['filesize']}<br />Width (px): {$d['filewidth']}<br />Height (px): {$d['fileheight']}<br />Filename: {$d['filename']}<br />Filetype: {$d['filetype']}<br />Filetag: {$d['filetag']}<br />MIME: {$d['filemime']}<br />Bits: {$d['filebits']}<br />Channels: {$d['filechannels']}<br />FileConstant: {$d['fileimgconstant']}<br /></td></tr>";   
$table .="</tbody></table>";
echo $table;
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.