Images can be stored to a MySQL database, in a BLOB datatype. While all other processes of uploading the image is the same as it is to a file, there are couple of error checking that is essential to ensure safety. The code that I have included here checks if the file uploaded is in a desired image format. It also checks the size of an image to ensure it doesn't exceed the maximum limit. And if all is well, the image is finally uploaded to the database and retrived dynamically thereby. You can either copy the codes that I've included here one by one or you can even download the zip file which includes the whole package and run it. [ (38.2KB)] Interface: SQL QUERY: (to create Database and table) -- -- Database: `imagedb` -- CREATE DATABASE `imagedb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE `imagedb`; -- -------------------------------------------------------- -- -- Table structure for table `image` -- CREATE TABLE `image` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) NOT NULL, `image` longblob NOT NULL, `type` varchar(32) NOT NULL, `size` varchar(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ; NOTE: You need to run this query first to create a database and a table.I have created a database "imagedb" and a table "image". We could have used the blob datatype here for image, but we have used longblob to accomodate larger images. Main Code: "index.php" Uploading image to MySQL database and displaying it
Upload Form