After building the table successfully, let's start doing some code. These lines below are sample of code to store image files to database.
<?php // index.php - by Hermawan Haryanto <hermawan@codewalkers.com> // Example PHP Script, demonstrating Storing Image in Database // Detailed Information can be found at http://www.codewalkers.com
// database connection $conn = mysql_connect("localhost", "username", "password") OR DIE (mysql_error()); @mysql_select_db ("database", $conn) OR DIE (mysql_error());
// Do this process if user has browse the // file and click the submit button if ($_FILES) { $image_types = Array ("image/bmp", "image/jpeg", "image/pjpeg", "image/gif", "image/x-png"); if (is_uploaded_file ($_FILES[“userfile”][“tmp_name”])) { $userfile = addslashes (fread (fopen ($_FILES["userfile"]["tmp_name"], "r"), filesize ($_FILES["userfile"]["tmp_name"]))); $file_name = $_FILES["userfile"]["name"]; $file_size = $_FILES["userfile"]["size"]; $file_type = $_FILES["userfile"]["type"];
// Do this process of user has click // a file name to view or remove if ($_GET) { $iid = $_GET["iid"]; $act = $_GET["act"]; switch ($act) { case rem: $sql = "DELETE FROM image WHERE image_id=$iid"; @mysql_query ($sql, $conn); Header("Location:./index.php"); exit(); break; default: print "<img src="image.php?iid=$iid">"; break; } }
Upload that file, and when you execute the index.php file you'll see a simple form. With that form you'll be able to select the image file you wish to upload, then click submit. The file is uploaded to database and then you'll have a list on the bottom of the form. That's the list of files which have been uploaded to your database.