How To Insert Images Into MySQL Database
Update 27 December 2008 - The following link offers a better example than the information appearing below:
PHP file upload form with image handling options including MySQL image insert.
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
<?php
// upload.cfg.php Config file for image upload application
// $opts = array(); // defined elsewhere along with a db connection
$opts['debug'] = 0;
if(!isset($opts['domain'])){
$opts['domain'] = str_replace('www.', '', $_SERVER['HTTP_HOST']);
}
$opts['cfg']['mime_img_restriction'] = array('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png', 'image/x-png');
$opts['cfg']['comment_reqd'] = 1;
$opts['cfg']['show_config_info'] = 0;
$opts['cfg']['delete_original'] = 1;
// To target specific subfolders of ./upl/ for different file types this must
// be redefined/appended in the for() loop inside the upload processing script (upload.proc.php)
$opts['cfg']['dir_name'] = './upl';
$opts['cfg']['expose_dir_name'] = 1;
$opts['cfg']['overwrite'] = 1;
$opts['comment']['delete'] = $opts['cfg']['delete_original'] > 0 ? 'will be deleted' : 'will NOT be deleted';
$opts['comment']['verbose'] = 0;
$opts['form']['bgcolor_outer_table'] = '#ffffff';
$opts['form']['bgcolor_inner_table'] = '#c0c0c0';
$opts['form']['browse_cols'] = 61;
$opts['form']['font_size'] = 'small';
$opts['form']['increment']['jpeg_compression'] = 5; // Incremental steps for the drop down SELECT boxes
$opts['form']['increment']['main_img_width'] = 20;
$opts['form']['increment']['thumb_width'] = 5;
$opts['form']['upload_fields'] = 3;
$opts['image']['max_file_size'] = 2048000; // maximum bytes as an integer (no commas); 2048000 is about 2-megabytes
$opts['image']['fmt_max_file_size'] = number_format($opts['image']['max_file_size']);
$opts['image']['type_ary'] = array( '0' => 'None', '1' => 'GIF', '2' => 'JPG', '3' => 'PNG', '4' => 'SWF', '5' => 'PSD', '6' => 'BMP', '7' => 'TIFF (Intel)', '8' => 'TIFF (Motorola)', '9' => 'JPC', '10' => 'JP2', '11' => 'JPX', '12' => 'JB2', '13' => 'SWC', '14' => 'IFF', '15' => 'WBMP', '16' => 'XBM' );
$opts['thumb']['min_width'] = 80;
$opts['cfg']['allowed_ext'] = array(
'avi',
'bmp',
'csv',
'doc',
'gif',
'iff',
'jb2',
'jp2',
'jpc',
'jpeg',
'jpg',
'jpx',
'mpeg',
'mpg',
'pdf',
'pjpeg',
'png',
'ppt',
'psd',
'swc',
'swf',
'tiff',
'txt',
'wav',
'wbmp',
'x-png',
'xbm',
'xls'
);
// Defaults for GET method
$create_thumb = 1;
$jpeg_compression = 75;
$max_width = 580; // Must be evenly divisible by $opts['form']['increment']['main_img_width']
$max_width_thumb = 150;
$mysql_thumb = 0;
$mysql_image = 0;
?>