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

<?php 

// upload.form.php Upload form for image upload application

if(!$gd_exists = @get_extension_funcs('gd')){

    
abort('The required GD library does not appear to exist on the server');

}

@require_once(
'upload.cfg.php');

if(
$opts['cfg']['show_config_info'] == 1){

?>

    <p><span style="font-size:<?php echo $opts['form']['font_size']; ?>">
    This application is currently configured to allow 
    <?php echo $opts['form']['upload_fields']; ?> 
    file(s) to be uploaded at one time. 
    Uploaded image files (JPEG/PNG/GIF) will be resampled and (if necessary) scaled down. 
    JPEG compression of 75% is recommended. 
    Maximum upload size is capped at <?php echo $opts['image']['fmt_max_file_size']; ?> bytes (2-MB). 
    Web-friendly filenames will be created by converting alphabetic characters to 
    lower case letters, and replacing spaces with the underscore character. 
    <?php echo $opts['cfg']['overwrite'] == ' Existing files of the same name will be overwritten.' ''?> 
    In the case of image files, the (temporary) uploaded file <?php echo $opts['comment']['delete']; ?> 
    after the thumbnail and a resampled image are created. (Originals are *not* stored online.) 
    Use the Browse button to select a file, then click Submit <span class="red">one time only</span>. 
    Processing may require 10-20 seconds depending on connection speed, file size, and current server load. 
    </span></p>

<?php }else{ ?>

    <p><span style="font-size:<?php echo $opts['form']['font_size']; ?>">Browse for your files, enter a caption, and click Submit. <br>Certain file types may be may be rejected (depending on the current configuration).</span></p>

<?php ?>


<form enctype="multipart/form-data" method="POST" action="<?php echo $opts['https_url'];?>results.php" id="form1" name="form1">

<p>
    <select name="upl_sub_dir" style="font-size:<?php echo $opts['form']['font_size']; ?>;background-color:#ffff00;color:#000000">
        <option value="">Destination Folder - Applies Only To Images Files</option>
        <option value="default">/upl/imgs</option>
        <option value="animal">/upl/imgs/animal</option>
        <option value="vegetable">/upl/imgs/vegetable</option>
        <option value="mineral">/upl/imgs/mineral</option>
    </select>
</p>

<p>Contact the Webmaster to have additional folders (albums) created.</p>

<table border="0" cellpadding="0" cellspacing="0" bgcolor="<?php echo $opts['form']['bgcolor_outer_table']; ?>">

<tr>

    <td>

<?php

// A heredoc "may" make it easier to put multiple form elements in an HTML table.
// Subsequently output the $upload_fields below using sprintf();
// Table width % needs to be %% since this is run through sprintf()

$upload_fields = <<<HEREDOC_VAR
<table width="100%%" style="margin:0px 0px 10px 0px" align="center" border="0" cellpadding="10" cellspacing="0" bgcolor="{$opts['form']['bgcolor_inner_table']}">
    <tr height="5"><td colspan="2" height="5" style="background-image:url('./imgs/chromey5px.jpg');background-repeat:repeat;padding:0px"><img width="1" height="1" border="0" alt="" src="imgs/spacer.gif"></td></tr>
   <tr>
      <td colspan="2">
         <input type="file" size="%s" name="toProcess
[%s]" style="font-size:%s" id="elem_a_%s">
         <!--                     ^                   ^                     ^              ^ -->
      </td>
   </tr>
   <tr>
      <td colspan="2">
      <input type="text" name="comment
[%s]" size="%s" maxlength="255" value="" style="font-size:%s" id="elem_b_%s"> 
      <!--                             ^          ^                                             ^               ^ -->
      <span style="font-size:%s">Caption</span>
      <!--                   ^              -->
      </td>
   </tr>
    <tr height="5"><td colspan="2" height="5" style="background-image:url('./imgs/chromey5px.jpg');background-repeat:repeat;padding:0px"><img width="1" height="1" border="0" alt="" src="imgs/spacer.gif"></td></tr>
</table>

HEREDOC_VAR;

for(
$i 0$i $opts['form']['upload_fields']; $i++){

    echo 
"\n".sprintf($upload_fields
        
$opts['form']['browse_cols'], 
            
$i
                
$opts['form']['font_size'], 
                    
$i
                        
$i
                            
$opts['form']['browse_cols']-10
                                
$opts['form']['font_size'], 
                                    
$i
                                        
$opts['form']['font_size'], 
                                            
$i
                                                
$opts['form']['font_size']
            );


};

?>

<table border="0" cellpadding="0" cellspacing="0" bgcolor="<?php echo $opts['form']['bgcolor_inner_table']; ?>" width="100%">

<tr><td style="padding:12px 10px">

    <table align="center" border="0" cellpadding="2" cellspacing="0">
        <tr>
            <td align="right">
             <select name="create_thumb" style="font-size:<?php echo $opts['form']['font_size']; ?>">
                    <option value="1"<?=$create_thumb == '1'?' selected':''?>>Yes</option>
                    <option value="0"<?=$create_thumb == '0'?' selected':''?>>No</option>
                </select>
            </td>
            <td>
                <span style="font-size:<?php echo $opts['form']['font_size']; ?>">Make thumbnails when uploading images (JPEG/PNG/GIF)</span>
            </td>
        </tr>
        <tr>
            <td align="right">
                <select name="jpeg_compression" style="font-size:<?php echo $opts['form']['font_size']; ?>">
                <?php
                
for($i 50$i 101$i += $opts['form']['increment']['jpeg_compression']){
                    
$sel $jpeg_compression != $i '' ' selected';
                    
printf("\t<option value=\"%02d\"%s>%s</option>\n"$i$sel$i);
                }
                
?>
                </select>
            </td>
            <td><span style="font-size:<?php echo $opts['form']['font_size']; ?>">&#37; Compression (JPEG images only)</span></td>
        </tr>
        <tr>
            <td align="right">
                <select name="max_width_thumb" style="font-size:<?php echo $opts['form']['font_size']; ?>">
                <?php
                
for($i $opts['thumb']['min_width']; $i < ($max_width_thumb 1); $i += $opts['form']['increment']['thumb_width']){
                    
$sel $max_width_thumb != $i '' ' selected';
                    
printf("\t<option value=\"%02d\"%s>%s</option>\n"$i$sel$i);
                }
                
?>
                </select>
            </td>
            <td><span style="font-size:<?php echo $opts['form']['font_size']; ?>">pixels, thumbnail maximum width</span></td>
        </tr>
        <tr>
            <td align="right">
                <select name="max_width" style="font-size:<?php echo $opts['form']['font_size']; ?>">

                <?php
                $start 
400;
                
$stop 640 1;

                for(
$i $start$i $stop$i += $opts['form']['increment']['main_img_width']){
                    
$sel $max_width != $i '' ' selected';
                    
printf("\t<option value=\"%02d\"%s>%s</option>\n"$i$sel$i);
                }
                
?>

                </select>
            </td>
            <td><span style="font-size:<?php echo $opts['form']['font_size']; ?>">pixels, image maximum width</span></td>
        </tr>

        <tr>
            <td align="right">
             <select name="mysql_thumb" disabled style="font-size:<?php echo $opts['form']['font_size']; ?>">
                    <option value="0"<?=$mysql_thumb == '0'?' selected':''?>>No</option>
                    <option value="1"<?=$mysql_thumb == '1'?' selected':''?>>Yes</option>
                </select>
            </td>
            <td>
                <span style="font-size:<?php echo $opts['form']['font_size']; ?>">Save thumbnails in MySQL</span>
            </td>
        </tr>

        <tr>
            <td align="right">
             <select name="mysql_image" disabled style="font-size:<?php echo $opts['form']['font_size']; ?>">
                    <option value="0"<?=$mysql_image == '0'?' selected':''?>>No</option>
                    <option value="1"<?=$mysql_image == '1'?' selected':''?>>Yes</option>
                </select>
            </td>
            <td>
                <span style="font-size:<?php echo $opts['form']['font_size']; ?>">Save images in MySQL</span>
            </td>
        </tr>
    </table>

  </td></tr></table>

<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $opts['image']['max_file_size']; ?>">

<input type="hidden" name="heading" value="Upload Comfirmation">

<input type="hidden" name="s" value="0">

<input type="hidden" name="snum" value="<?php echo $opts['snum']; ?>">

<input type="hidden" name="submit_token" value="<?php echo $submit_token?>">

<input type="hidden" name="submit_check" value="1">

<input type="hidden" name="what" value="upload">

<div align="center" style="margin:8px 0px 0px 0px"><input type="submit" value="Submit" class="pme-add"></div>

</td></tr></table>

</form>

<p><span style="font-size:<?php echo $opts['form']['font_size']; ?>">Contact the webmaster if modifications become necessary.</span></p>

The resulting upload form will look something like this:

Browse for your files, enter a caption, and click Submit.
Certain file types may be may be rejected (depending on the current configuration).

action="#" id="form1" name="form1">

Contact the Webmaster to have additional folders (albums) created.

Caption
Caption
Caption
Make thumbnails when uploading images (JPEG/PNG/GIF)
% Compression (JPEG images only)
pixels, thumbnail maximum width
pixels, image maximum width
Save thumbnails in MySQL
Save images in MySQL

Here is the background image for the form:
hockinson.com

phpvs.com - hockinson.com - cymaxmedia.com - phpmyedit.org - platon.sk - denvermetro.org - theirdreams.com - mysql / php form generator