----- Original Message ----- 
From: "raquibul islam"
> Will you please mail me a example code ? I mean some pages like. 1 upload 
> page and another page for show the image.

Hi Raquibul,
I've just tried a very basic example from a book.
It's in no-way fit to use as it is, and only an *example* but it may get you 
started.
I'm presuming you know how to open a database.
The book *recommends* that images should not be stored in DB's, and would be 
better to store the image names instead, if you're trying to hide them.

# Create a simple table like so:
CREATE TABLE `images` (
  `image_id` int(10) unsigned NOT NULL auto_increment,
  `image` blob,
  `image_type` varchar(10) NOT NULL default '',
  KEY `image_id` (`image_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;


# Example page to upload file:
<html>
<body>
<?php
if (isset($_POST['submit']))
{
  require ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/open_your_database.php');

  $image = addslashes(fread(fopen($_FILES['the_file']['tmp_name'], 'r'), 
$_FILES['the_file']['size']));
  $query = "INSERT INTO images VALUES (0, '$image', '" . 
$_FILES['the_file']['type'] . "')";

  if (mysql_query($query)) echo '<p>Image No: ' . mysql_insert_id() . ' has 
been stored</p>';
  else echo 'Image could not be stored ' . mysql_error();
  mysql_close();
}
else
{
?>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" 
enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  <div align="center">
  Select a file to upload: <input type="file" name="the_file" /><br />
  <input type="submit" name="submit" value="Submit" />
  </div>
  </form>
<?php
}
?>
</body>
</html>


# Page to get the image from DB:
# You can't display it directly in an html page, as 2 headers would be sent, as 
someone explained previously.
<?php
if (isset($_GET['i']))
{
  require ($_SERVER['DOCUMENT_ROOT'] . '/cgi-bin/open_your_database.php');
  
  $query = "SELECT image, image_type FROM images WHERE image_id=" . $_GET['i'];
  if ($query_result = mysql_query($query))
  {
    $image = mysql_fetch_array($query_result);
    header("Content-type: $image[1]");
    echo $image[0];
  }
  mysql_close();
}
?>


# Display image in an html page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div><img src="image.php?i=1" alt="Image description" /></div>
</body>
</html>

Hope this helps you?
Regards, Bob.


Reply via email to