Upload image ke mysql

bagaimana cara memasukkan, hapus gambar ke mysql? d mysql udah d sediain tipe data blob(kirain untuk apa) ini untuk image. mau script php nya ga? nih, aku juga dapet dari googling :)

<?php
$db_host = 'localhost'; // don't forget to change
$db_user = 'root';
$db_pwd = '';
$database = 'dbku';
$table = 'galery_up';
// use the same name as SQL table
$password = 'abc';
// 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_up='$ext', title_up='$title', data_up='$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_up=$id");
      $msg = 'Photo deleted';
   }
  }}
  elseif (isset($_GET['show'])){
   $id = intval($_GET['show']);
   $result = mysql_query("SELECT ext_up, UNIX_TIMESTAMP(image_time), data_up FROM {$table} WHERE id_up=$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();
    }
?>
<html>
<body bgcolor="7EA9D3" text="#000000" link="#000000" vlink="#000000" alink="#000000" topmargin="0" leftmargin="2" marginwidth="0" marginheight="0">
<div align="center">
  <center>
<table width="100%" bgcolor="#006699">
<tr>
 <td>&nbsp;</td>
 <td width="60%"><font color="#FFFFFF" size="+2">Upload Image</font></td>
 <td width="40%" align="right"><font color="#FFFFFF" size="+1">IIP</font></td>
 <td>&nbsp;</td>
</tr>
</table>
<table border="0" cellspacing="1" width="100%">
<?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
}
?>
<tr>
<td width="10%" align="left" colspan="2">
<h2>Uploaded images:</h2>
</td>
</tr>
<form action="<?=$PHP_SELF?>" method="post">
<!-- This form is used for image deletion -->
<?php
$result = mysql_query("SELECT id_up, image_time, title_up FROM {$table} ORDER BY id_up 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 "<tr>";
  echo "<td align='left' width='22%'>";
  echo "<li><input type='radio' name='del' value='{$id}'>";
  echo "<a href='{$PHP_SELF}?show={$id}' target='_blank'>{$title}</a> &ndash; ";
  echo "<small>;{$image_time}</small></li>";
  echo "</td>";
  echo "</tr>";
  }
//  echo '</ul>';
  echo "<table>";
  echo "<tr>";
  echo "<td width='10%' align='right'>";
  echo '<label for="password">Password:</label><br>';
  echo "</td>";
  echo "<td align='left'>";
  echo '<input type="password" name="password" id="password"><br><br>';
  echo "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td colspan='44' align='left'>";
  echo '<input type="submit" value="Delete selected">';
  echo "</td>";
  echo "</tr>";
  echo "</table>";
 }
  ?>
</form>
</table>

<table border="0" cellspacing="1" width="100%">
<tr>
<td width="10%" align="left" colspan="2">
<h2>Upload new image:</h2>
</td>
</tr>

<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">

<tr>
<td width="10%" align="left">
<label for="title">Title:</label></td>
<td>
<input type="text" name="title" id="title" size="33"></td></tr>
<tr>
<td width="10%" align="left">
<label for="photo">Image:</label></td>
<td>
<input type="file" name="photo" id="photo"></td></tr>
<tr>
<td width="10%" align="left">
<label for="password">Password:</label></td>
<td>
<input type="password" name="password" id="password"></td></tr>
<tr>
<td width="10%" align="center">
<input type="submit" value="upload"></td></tr>
</form>
</table>
</center>
</div>
</body>
</html>

mudah2an bermanfaat… thx u/ yg buat :D

2 Responses to “Upload image ke mysql”

  1. orenholic Says:

    makasih tutorialnya bang…

  2. Gebok Says:

    bukan cuma image, document juga kayaknya bisa di tampung pada type data blob.

    ===================================================

    masa sih bisa? tar dh d coba… :D

Leave a Reply