Hi, i had upload image to mySQL database...
The problem is i unable to open or view the image that uploaded.
May i know how display image from database by selectted id ???


the picture below is my "upload" table...
Appreciate for you help

Recommended Answers

All 10 Replies

like this:

$imagecontent = $row[content];
header("Content-type: .$row[type].");
print $imagecontent ;

like this:

$imagecontent = $row[content];
header("Content-type: .$row[type].");
print $imagecontent ;

thanks for your help ...but it display a blank page ...i dont know why...

here is image.php

<?php  

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="1234"; // Mysql password 
$db_name="new"; // Database name 

// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
			
$query  = "SELECT * FROM upload "  ;
$result = mysql_query($query) or die("Query failed ($query) - " . mysql_error());	
?>
				
<table width="1000" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1000" border="1" cellspacing="0" cellpadding="3">
<tr>
<strong>List of Case Procedure </strong> 
</tr>

<tr>
<td align="center" ><strong>id</strong></td>
<td align="center" ><strong>name</strong></td>
<td align="center"><strong>type</strong></td>

<td align="center" ><strong>Update</strong></td>
</tr>

<?php 
while($rows=mysql_fetch_array($result)){
?>
<tr>

<td><?php echo $rows["id"]; ?></td>
<td><?php echo $rows["name"];?></td>
<td><?php echo $rows["type"]; ?></td>

<td ><a href="view.php?id=<?php echo $rows['id']; ?>">Display</a></td>
</tr>

<?php
}
?>
<?php
mysql_close($con);
?>

this is view.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="1234"; // Mysql password 
$db_name="new"; // Database name 
//$tbl_name="action"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar

$id =$_GET['id'];

$query  = "SELECT type, content FROM upload WHERE id= $id";
$result = mysql_query($query) or die("Query failed ($query) - " . mysql_error());		
$imagecontent = $row[content];
header("Content-type: .$row[type].");
print $imagecontent ;

mysql_close();
?>

Can u help me find out the problem in my coding??

try to change these lines like as shown below:

$imagecontent = $row['content'];
$type=$row['type'];
header("Content-type: .$type.");
print $imagecontent ;

try to change these lines like as shown below:

$imagecontent = $row['content'];
$type=$row['type'];
header("Content-type: .$type.");
print $imagecontent ;

i still get the same result --------blank page , nothing display in web browser...
which part i did wrong??

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="1234"; // Mysql password 
$db_name="new"; // Database name 
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar 
$id =$_GET['id'];


$query  = "SELECT type, content FROM upload WHERE id= $id ";
$result = mysql_query($query) or die("Query failed ($query) - " . mysql_error());	
	$imagecontent = $row['content'];
	$type=$row['type'];s
	header("Content-type: .$type.");
	print $imagecontent ;
	
mysql_close();
?>

in the above code where is the $row defined.
i think it is missing.Please check.
and print mysql_num_rows($result); , whether it is returning rows or not.
and also check your insert query once.

post if you still need help i've just worked out how to do this myself

Member Avatar for diafol
$type=$row['type'];s
	header("Content-type: .$type.");

to this:

$type=$row['type'];
header("Content-type: $type");

??

You could also print out your query to see if it is retrieving what you think it should.

http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml this the source and I will try to explain as best as i can.

so in your database table as its pretty similar you just need to alter the fields.

`id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(64) character SET utf8 NOT NULL,
  `ext` varchar(8) character SET utf8 NOT NULL,
  `image_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `data` mediumblob NOT NULL,
  PRIMARY KEY  (`id`)

next bite is easy just copy this php above code any html.

<?php
$db_host = 'localhost'; // don't forget to change 
$db_user = 'mysql-user'; 
$db_pwd = 'mysql-password';

$database = 'test';
$table = 'ae_gallery';
// use the same name as SQL table

$password = '123';
// simple upload restriction,
// to disallow uploading to everyone


if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// This function makes usage of
// $_GET, $_POST, etc... variables
// completly safe in SQL queries
function sql_safe($s)
{
    if (get_magic_quotes_gpc())
        $s = stripslashes($s);

    return mysql_real_escape_string($s);
}

// If user pressed submit in one of the forms
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // cleaning title field
    $title = trim(sql_safe($_POST['title']));

    if ($title == '') // if title is not set
        $title = '(empty title)';// use (empty title) string

    if ($_POST['password'] != $password)  // cheking passwors
        $msg = 'Error: wrong upload password';
    else
    {
        if (isset($_FILES['photo']))
        {
            @list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
            // Get image type.
            // We use @ to omit errors

            if ($imtype == 3) // cheking image type
                $ext="png";   // to use it later in HTTP headers
            elseif ($imtype == 2)
                $ext="jpeg";
            elseif ($imtype == 1)
                $ext="gif";
            else
                $msg = 'Error: unknown file format';

            if (!isset($msg)) // If there was no error
            {
                $data = file_get_contents($_FILES['photo']['tmp_name']);
                $data = mysql_real_escape_string($data);
                // Preparing data to be used in MySQL query

                mysql_query("INSERT INTO {$table}
                                SET ext='$ext', title='$title',
                                    data='$data'");

                $msg = 'Success: image uploaded';
            }
        }
        elseif (isset($_GET['title']))      // isset(..title) needed
            $msg = 'Error: file not loaded';// to make sure we've using
                                            // upload form, not form
                                            // for deletion


        if (isset($_POST['del'])) // If used selected some photo to delete
        {                         // in 'uploaded images form';
            $id = intval($_POST['del']);
            mysql_query("DELETE FROM {$table} WHERE id=$id");
            $msg = 'Photo deleted';
        }
    }
}
elseif (isset($_GET['show']))
{
    $id = intval($_GET['show']);

    $result = mysql_query("SELECT ext, UNIX_TIMESTAMP(image_time), data
                             FROM {$table}
                            WHERE id=$id LIMIT 1");

    if (mysql_num_rows($result) == 0)
        die('no image');

    list($ext, $image_time, $data) = mysql_fetch_row($result);

    $send_304 = false;
    if (php_sapi_name() == 'apache') {
        // if our web server is apache
        // we get check HTTP
        // If-Modified-Since header
        // and do not send image
        // if there is a cached version

        $ar = apache_request_headers();
        if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists
            ($ar['If-Modified-Since'] != '') && // not empty
            (strtotime($ar['If-Modified-Since']) >= $image_time)) // and grater than
            $send_304 = true;                                     // image_time
    }


    if ($send_304)
    {
        // Sending 304 response to browser
        // "Browser, your cached version of image is OK
        // we're not sending anything new to you"
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304);

        exit(); // bye-bye
    }

    // outputing Last-Modified header
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $image_time).' GMT',
            true, 200);

    // Set expiration time +1 year
    // We do not have any photo re-uploading
    // so, browser may cache this photo for quite a long time
    header('Expires: '.gmdate('D, d M Y H:i:s',  $image_time + 86400*365).' GMT',
            true, 200);

    // outputing HTTP headers
    header('Content-Length: '.strlen($data));
    header("Content-type: image/{$ext}");

    // outputing image
    echo $data;
    exit();
}
?>

change this code to fit your database.

next code just make it fit your website.

<html><head>
<title>MySQL Blob Image Gallery Example</title>
</head>
<body>
<?php
if (isset($msg)) // this is special section for
                 // outputing message
{
?>
<p style="font-weight: bold;"><?=$msg?>
<br>
<a href="<?=$PHP_SELF?>">reload page</a>
<!-- I've added reloading link, because
     refreshing POST queries is not good idea -->
</p>
<?php
}
?>
<h1>Blob image gallery</h1>
<h2>Uploaded images:</h2>
<form action="<?=$PHP_SELF?>" method="post">
<!-- This form is used for image deletion -->

<?php
$result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER BY id DESC");
if (mysql_num_rows($result) == 0) // table is empty
    echo '<ul><li>No images loaded</li></ul>';
else
{
    echo '<ul>';
    while(list($id, $image_time, $title) = mysql_fetch_row($result))
    {
        // outputing list
        echo "<li><input type='radio' name='del' value='{$id}'>";
        echo "<a href='{$PHP_SELF}?show={$id}'>{$title}</a> &ndash; ";
        echo "<img src='{$_SERVER['PHP_SELF']}?show={$id}' width='140' height='140' />";
        echo "<small>{$image_time}</small></li>";
    }

    echo '</ul>';

    echo '<label for="password">Password:</label><br>';
    echo '<input type="password" name="password" id="password"><br><br>';

    echo '<input type="submit" value="Delete selected">';
}
?>

</form>
<h2>Upload new image:</h2>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">
<label for="title">Title:</label><br>
<input type="text" name="title" id="title" size="64"><br><br>

<label for="photo">Photo:</label><br>
<input type="file" name="photo" id="photo"><br><br>

<label for="password">Password:</label><br>
<input type="password" name="password" id="password"><br><br>

<input type="submit" value="upload">
</form>
</body>
</html>

red part need to be correct ahref as we to load new page with image and img to display the image on your web page.

this is a nice code if i could thank the coder i would saved me a tonne of work for a personal project.

dear trueedmar
i following you step but it show
Forbidden

You don't have permission to access /Bidot.com/< on this server.
any idea how to fix?

i've not taken this code live its only been tested on xampp. screen shot the error screen so i can take a proper troubleshooting look.

sorry bad grammar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.