mulitple image file into mysql

Discussion in 'PHP' started by beermaker74, Sep 22, 2006.

  1. #1
    Hello all,
    this is my first post. I am a complete newbie at this. I have this problem that I am sure is easy to fix. I need a user to be able to upload 20 images to my mysql database from the web. I have read that this is not a good idea because of server load etc. This is not an issue for me. I need to input about 30 text fields and 20 images in blob. I can input one image and one description with this script

    //
    <?php
    if ($action == "upload") {
    // ok, let's get the uploaded data and insert it into the db now
    include "open_db.inc";

    if (isset($binFile) && $binFile != "none") {
    $data = addslashes(fread(fopen($binFile, "r"), filesize($binFile)));
    $strDescription = addslashes(nl2br($txtDescription));
    $sql = "INSERT INTO binary_data ";
    $sql .= "(description, bin_data, filename, filesize, filetype) ";
    $sql .= "VALUES ('$strDescription', '$data', ";
    $sql .= "'$binFile_name', '$binFile_size', '$binFile_type')";
    $result = mysql_query($sql, $db);

    echo "Thank you. The new file was successfully added to our database.<br><br>";
    echo "<a href='main.php'>Continue</a>";
    }
    mysql_close();

    } else {
    ?>
    <HTML><style type="text/css">
    <!--
    body {
    background-color: #FFFFCC;
    }
    -->
    </style><title>Sql image upload form works</title>
    <BODY>
    <FORM METHOD="post" ACTION="add.php" ENCTYPE="multipart/form-data">
    <div align="center">
    <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="2000000">
    <INPUT TYPE="hidden" NAME="action" VALUE="upload">
    Welcome to the image upload page. Just upload away </div>
    <TABLE BORDER="1" align="center">
    <TR>
    <TD>Description: </TD>
    <TD><textarea name="txtDescription" rows="5" cols="50"></textarea></TD>
    </TR>
    <TR>
    <TD>File: </TD>
    <TD><INPUT TYPE="file" NAME="binFile"></TD>
    </TR>
    <TR>
    <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
    </TR>
    </TABLE>
    </FORM>
    </BODY>
    </HTML>
    <?php
    }
    ?>

    can I adjust this script to have the 20 image fields and 20 descriptions that I need. I just can't figure out what I need to change. If there is some other script that I should use then let me know. I am trying my best to learn this. I thank you for your time
     
    beermaker74, Sep 22, 2006 IP
  2. esoomllub

    esoomllub Well-Known Member

    Messages:
    279
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #2
    The general approach would be:

    - modify the html to have 20 separate inputs and files, name them either with a number on the end (binfile1, etc), or preferably as an array (binfile[] -- each would have the same name).

    - modify your script to handle either each possible number $binfile1 -> $binfile20 (in a loop of $_POST where the key started with "binfile") OR if you chose the array option for naming binfile[], you could just "foreach" loop through the binfile array.

    I prefer the array route as it makes the code a much more efficient loop in my mind.
     
    esoomllub, Sep 22, 2006 IP
  3. vinxxv

    vinxxv Well-Known Member

    Messages:
    506
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    118
    #3
    Hi.. just to give you an idea beermaker74... correct me if i'm wrong esoomllub.

    
    <?
         if (isset($_POST['Submit'])){
    	 	$max = 4; //change this to 20
            for ($i=0;$i<$max;$i++){
               //change this part with the INSERT to DB function
    	       echo $_FILES['binfile']['name'][$i]." : ".$_POST['description'][$i]."<br>";
    	    }
         }
    ?>
    <html>
       <body>
        <form action="<?=$PHP_SELF?>" enctype="multipart/form-data" method="post" >
            1: <input type='file' name='binfile[]'> | Desc here: <input type="text" name="description[]"><br>
            2: <input type='file' name='binfile[]'> | Desc here: <input type="text" name="description[]"><br>
            3: <input type='file' name='binfile[]'> | Desc here: <input type="text" name="description[]"><br>
            4: <input type='file' name='binfile[]'> | Desc here: <input type="text" name="description[]"><br>
            .....Do this til 20 .......<br>
    
            <input type="submit" name="Submit" value="Upload Files">
    
        </form>
       </body>
    </html>
    
    Code (markup):
     
    vinxxv, Sep 22, 2006 IP
  4. beermaker74

    beermaker74 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    idont see post anywhere in the code that i posted. The codethat people have posted gets me the image upload form but it doesnt upload anywhere. It seems the addslash and the codeabove that does something to the image data. Can it be modified to add more fields. I can put 20 file fields in the form with dreamweaver. I just dont know where to point them with this script. The code dreamweaver writesfor me when I insert record behavior is post but the above code soesnt use that. I am so frustrated. I have read a million tutorials and still cant figure this out. I just want it to be a simple upload page where ther can input text and images. There has tobe some easy way to do this. I greatly appreciate your help and apologize for my stupidity.
     
    beermaker74, Sep 22, 2006 IP
  5. esoomllub

    esoomllub Well-Known Member

    Messages:
    279
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #5
    You are not ignorant. While everyone says PHP is such an easy scripting language to learn (and it generally is), if you are not a programmer by nature, doing anything beyond basic is not necessarily an easy task.

    $_POST is a variable that is automatically created by php, and is more secure to use than just a variable name of the form value.

    $_FILES is automatically created when files are uploaded. The only change I would make to vinxxv's cod is to loop through the $_FILES array with a foreach, which would then be able to handle any number of uploaded files if you decide to change it in the future. That's just a personal preference though.

    
        if (isset($_POST['Submit'])){
            foreach( $_FILES['binname']['name'] as $k => $v ) {
               // change this part with the INSERT to DB function
               // OR whatever else you want to do with the file.
    	   echo $v[$k] . " : " . $_POST['description'][$k]."<br/>";
    	}
         }
    
    PHP:
    Note that whenever you are uploading files... you need to pay special attention to security. Verify that you are getting image files of the type you will allow.
     
    esoomllub, Sep 22, 2006 IP