uploading multiple images in PHP/MySQL?

Discussion in 'PHP' started by peppy, Jun 23, 2010.

  1. #1
    Greetings,

    I have a question that should be very simple for many of you who have a form that uploads multiple images. I found this script while searching on Google:
    http://www.webdeveloper.com/forum/showthread.php?t=96532

    My HTML Form where people can upload two images:

    <form action="" method="post" name="addcoin" id="addcoin" enctype="multipart/form-data">
                <table width="100%" border="1" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                  <tr>
                    <td>Upload Images:</td>
                  </tr>
                  <tr>
                    <td>Obverse: (Front or "Heads" side of coin) <input id="obverse" type="file" name="file[]"></td>
                  </tr>
                  <tr>
                    <td>Reverse: (Back or "Tails" side of coin) <input id="reverse" type="file" name="file[]"></td>
                  </tr>
                  <tr>
                    <td><center>
                        <input type="submit" name="submit" value="submit" />
                    </center></td>
                  </tr>
                </table>
              </form>
    PHP:

    My Processor code that is supposed to store my images into a file and assign a filename:

    <?php
    ##########################################################################################################################################################
    // filename: upload.processor.php
    
    // first let's set some variables
    
    // make a note of the current working directory, relative to root.
    $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
    
    // make a note of the directory that will recieve the uploaded files
    $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'images/';
    
    // make a note of the location of the upload form in case we need it
    $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.form.php';
    
    // make a note of the location of the success page
    $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.success.php';
    
    // name of the fieldname used for the file in the HTML form
    $fieldname = 'file';
    
    //echo'<pre>';print_r($_FILES);exit;
    
    
    
    // Now let's deal with the uploaded files
    
    // possible PHP upload errors
    $errors = array(1 => 'php.ini max file size exceeded',
                    2 => 'html form max file size exceeded',
                    3 => 'file upload was only partial',
                    4 => 'no file was attached');
    
    // check the upload form was actually submitted else print form
    isset($_POST['submit'])
        or error('the upload form is neaded', $uploadForm);
        
    // check if any files were uploaded and if
    // so store the active $_FILES array keys
    $active_keys = array();
    foreach($_FILES[$fieldname]['name'] as $key => $filename)
    {
        if(!empty($filename))
        {
            $active_keys[] = $key;
        }
    }
    
    // check at least one file was uploaded
    count($active_keys)
        or error('No files were uploaded', $uploadForm);
            
    // check for standard uploading errors
    foreach($active_keys as $key)
    {
        ($_FILES[$fieldname]['error'][$key] == 0)
            or error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm);
    }
        
    // check that the file we are working on really was an HTTP upload
    foreach($active_keys as $key)
    {
        @is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
            or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);
    }
        
    // validation... since this is an image upload script we
    // should run a check to make sure the upload is an image
    foreach($active_keys as $key)
    {
        @getimagesize($_FILES[$fieldname]['tmp_name'][$key])
            or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);
    }
        
    // make a unique filename for the uploaded file and check it is
    // not taken... if it is keep trying until we find a vacant one
    foreach($active_keys as $key)
    {
        $now = time();
        while(file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
        {
            $now++;
        }
    }
    
    // now let's move the file to its final and allocate it with the new filename
    foreach($active_keys as $key)
    {
        @move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
            or error('receiving directory insuffiecient permission', $uploadForm);
    }
    
    // If you got this far, everything has worked and the file has been successfully saved.
    // make an error handler which will be used if the upload fails
    function error($error, $location, $seconds = 5)
    {
        echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
        '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
        '<html lang="en">'."\n".
        '    <head>'."\n".
        '        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
        '        <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
        '    <title>Upload error</title>'."\n\n".
        '    </head>'."\n\n".
        '    <body>'."\n\n".
        '    <div id="Upload">'."\n\n".
        '        <h1>Upload failure</h1>'."\n\n".
        '        <p>An error has occured: '."\n\n".
        '        <span class="red">' . $error . '...</span>'."\n\n".
        '         The upload form is reloading</p>'."\n\n".
        '     </div>'."\n\n".
        '</html>';
        exit;
    } // end error handler 
    ##########################################################################################################################################################
    ?>
    PHP:
    All I would like to do is put an MySQL statement that will insert the filename of the "Obverse" picture into the 'obverse' database field and insert the filename of the "Reverse" picture into the 'reverse' database field.

    Thanks
     
    peppy, Jun 23, 2010 IP
  2. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Nevermind, I think I had a moment of "zen" and figured it out myself.

    Thanks for looking.
     
    peppy, Jun 23, 2010 IP
  3. Abhilash raj

    Abhilash raj Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #3
    can you tell me how to add the file name to mysql
     
    Abhilash raj, Jun 9, 2013 IP
  4. abhijitnumber1

    abhijitnumber1 Member

    Messages:
    19
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    38
    #4
    $your_file_name = 'image.png';//your file name
    Table Structure 'id(auto increment)', 'file_name' (I create only two column, but you can create as many you like)
    $insert_data_into_database = "INSERT INTO your_table_name VALUES('','".$your_file_name."')";
    mysql_query($insert_data_into_database); //send a mysql query
    PHP:
     
    abhijitnumber1, Jul 8, 2013 IP
  5. abhijitnumber1

    abhijitnumber1 Member

    Messages:
    19
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    38
    #5
    As i see You don't specify any table structure, so i create two tables
    1. obverse -> id (auto increment), obverse file name (This are Column of obverse table)
    2. reverse -> id(auto increment), reverse file name (This are column of reverse table)

    $obverse_file_name = 'front_image.jpg';
    $reverse_file_name = 'back_image.jpg';
     
    $insert_into_o = "INSERT INTO obverse VALUES('','".$obverse_file_name."')";
    mysql_query($insert_into_o);
     
    $insert_into_r = "INSERT INTO reverse VALUES('','".$reverse_file_name."')";
    mysql_query($insert_into_r);
    PHP:
     
    abhijitnumber1, Jul 8, 2013 IP