How To Insert Images Into MySQL Database
Note: this is not a complete project. No support is offered.
Related scripts might provide some ideas to those of you who building your own File Upload Application, or wanting to insert images in a MySQL database table. These examples may be missing some certain (minor) functions that are part of the CMS from which these examples were extracted.
These scripts were extracted a CMS in the scenario that a user is logged in and wants to upload both image files and non-image files. An attempt is made to sort the incoming files in subdirectories located under a ./upl/ directory (based on MIME type and/or filename extension). And, optionally insert an uploaded image and/or image thumbnail in MySQL.
Part 1 of 5 - Part 2 of 5 - Part 3 of 5 - Part 4 of 5 - Part 5 of 5
# BLOB storage
CREATE TABLE IF NOT EXISTS `vs_images1` (
`id` mediumint() unsigned NOT NULL auto_increment,
`user_id` mediumint() NOT NULL default '0',
`category` enum('image','thumb','other') NOT NULL default 'image',
`image_caption` varchar(255) NOT NULL default '',
`image` blob NOT NULL,
`image_width` smallint() unsigned NOT NULL default '0',
`image_height` smallint() unsigned NOT NULL default '0',
`image_type1` tinyint() unsigned NOT NULL default '0',
`image_type2` varchar(20) NOT NULL default '',
`img_attrib` varchar(30) NOT NULL default '',
`hidden` tinyint() unsigned NOT NULL default '0',
`deleted` tinyint() unsigned NOT NULL default '0',
`updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
# Tracking uploaded file info (not storing BLOBS)
CREATE TABLE IF NOT EXISTS `vs_user_uploads` (
`upld_id` smallint() unsigned NOT NULL auto_increment,
`user_id` char(6) NOT NULL default '',
`user_name` char(20) NOT NULL default '',
`comment` char(255) NOT NULL default '',
`upld_time` char(10) NOT NULL default '',
`dir` char(255) NOT NULL default '',
`filename` char(50) NOT NULL default '',
`ext` char(5) NOT NULL default '',
`mime_type` char(50) NOT NULL default '',
`category` enum('image','thumb','other') NOT NULL default 'image',
`html_tag` char(255) NOT NULL default '',
`img_width` char(4) NOT NULL default '',
`img_height` char(4) NOT NULL default '',
`img_type_no` char(2) NOT NULL default '',
`img_type_txt` char(20) NOT NULL default '',
`img_src_attr` char(30) NOT NULL default '',
`hidden` enum('0','1') NOT NULL default '0',
`deleted` enum('0','1') NOT NULL default '0',
`updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`upld_id`),
KEY `user_id` (`user_id`),
KEY `user_name` (`user_name`)
) ENGINE=MyISAM;