0

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....

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by diafol
0

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"/>
</form>

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:

include("db.php");

//check form is sent
if($_POST){
    //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
        mysql_query($sql);
        //Check to see if the query is successful
        if(mysql_affected_rows()>0){
            echo "successfully stored";
        }else{
            echo "couldn't store the image";    
        }
    }else{
        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:

if(isset($_GET['img'])){
    $id = intval($_GET['img']);
    include("db.php");

    $sql = "SELECT * FROM images WHERE image_id = $id";
    $r = mysql_query($sql);
    if(mysql_num_rows($r)){
        $d = mysql_fetch_assoc($r);
        header("Content-type: {$d['filemime']}");
        echo $d['img'];
    }else{
        //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);
        if(mysql_num_rows($r)){
            $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 -->

<?php 
//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;
?>

Edited by diafol

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.