Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple image uploads
01-23-2005, 05:32 AM (This post was last modified: 01-23-2005 05:41 AM by Jonathan.)
Post: #1
Sad Multiple image uploads
I recently started learning PHP, so I\'m what you could call a big n00b at it still. Nonetheless , I\'m trying to create something...


What I want to achieve:
I want to have a script with 4 upload fields & 1 submit button to simultaniously upload 4 images to a folder somewhere on the server. - Done, no problem

But I also need the filename of each image to be stored in the database in a different row, under the same ID - Not done, and my big problem

(see image below for some more clarification)
[Image: images_table.gif]


Some extra info:
Let\'s assume our files are image1.jpg, image2.jpg, image3.jpg & image4.jpg

The script uploads the files and inserts their filename in the database, but it inserts:

- image1.jpg under row image0 in id 4
- image2.jpg under row image1 in id 5
- image3.jpg under row image2 in id 6
- image4.jpg under row image3 in id 7

(see image below for some more clarification)
[Image: images_table2.gif]

This is what I have for my database, I fear the problem may lie here?
Code:
CREATE TABLE test (
  id tinyint (4) NOT NULL auto_increment,
  image0 varchar (250) NOT NULL default \'\',
  image1 varchar (250) NOT NULL default \'\',
  image2 varchar (250) NOT NULL default \'\',
  image3 varchar (250) NOT NULL default \'\',
  PRIMARY KEY (id),
  UNIQUE id (id)
);

INSERT INTO test VALUES (2,\'yarr0.jpg\',\'yarr1.jpg\',\'yarr2.jpg\',\'yarr3.jpg\');
INSERT INTO test VALUES (3,\'yii0.jpg\',\'yii1.jpg\',\'yii2.jpg\',\'yii3.jpg\');

This is my php code so far

Code:
<html>
<body>

<?php require (\'config.php\'); // data to connect to the DB ?>

<?php    
    echo \"<form name=\\\"image_upload\\\" enctype=\\\"multipart/form-data\\\" method=\\\"post\\\" action=\\\"$PHP_SELF\\\">\\n\";

    $fields = 4; // number of upload fields
    
    for($x=0;$x<$fields;$x++){ // start of dynamic form (loop)
        echo \"<input name=\\\"image$x\\\" size=\\\"50\\\" type=\\\"file\\\" id=\\\"image$x\\\"><br>\\n\";

    } // end for loop to create 4 input fields
        echo \"<input type=\\\"submit\\\" name=\\\"submit\\\" value=\\\"Verstuur\\\">\\n\";
        echo \"</form>\";

if($_POST[\'submit\']) { // form has been submitted
    
    for($x=0;$x<$fields;$x++){ // start for loop
    
    $file_name = $_FILES[\'image\'. $x][\'name\'];
    $uploads = \'upload\'; // directory to upload to
    $copy = copy($_FILES[\'image\'. $x][\'tmp_name\'],$uploads.\'/\'.$file_name);
        
        if($copy) { // check if successfuly copied
            $sql = \"INSERT INTO test (image$x) VALUES (\'$file_name\')\";    
            $result = mysql_query($sql); // run SQL query against DB
            echo \"$file_name | uploaded successfuly!<br>\\n\";
    
        }else{
            echo \"$file_name | could not be uploaded!<br>\\n\";
        }
    
    } // end for loop
    
} // end of submit statement


?>

</body>
</html>

I know there\'s no virtually no validation yet, but my first priority is to get it to work. If there\'s anyone that can help me, please have a look because I\'m in dire need of this script!

Thanks in advance.
Visit this user's website Find all posts by this user
Quote this message in a reply
01-23-2005, 06:02 AM
Post: #2
RE: Multiple image uploads
Spunge = Ownage.

I can\'t help you, but I do like multiple image uploaders.
Find all posts by this user
Quote this message in a reply
01-23-2005, 06:38 AM (This post was last modified: 01-23-2005 09:06 AM by Jackeh.)
Post: #3
RE: Multiple image uploads
Code:
<html>
<body>

<?php require (\'config.php\'); // data to connect to the DB ?>

<?php
    echo \"<form name=\\\"image_upload\\\" enctype=\\\"multipart/form-data\\\" method=\\\"post\\\" action=\\\"$PHP_SELF\\\">\\n\";

    $fields = 4; // number of upload fields

    for($x=0;$x<$fields;$x++){ // start of dynamic form (loop)
        echo \"<input name=\\\"image$x\\\" size=\\\"50\\\" type=\\\"file\\\" id=\\\"image$x\\\"><br>\\n\";

    } // end for loop to create 4 input fields
        echo \"<input type=\\\"submit\\\" name=\\\"submit\\\" value=\\\"Verstuur\\\">\\n\";
        echo \"</form>\";

if($_POST[\'submit\']) { // form has been submitted

        $num = \'0\';
        $sql = \"SELECT * from `test`\";
        $sql_result = mysql_query($sql) or die(\"Couldn\'t execute query.\");
        while ($row = mysql_fetch_array($sql_result)) {
          $id = $row[\"id\"];
if ($id > $num) {
$num == $id;
}
    }
        $sql = \"INSERT INTO test (id) values (\'$num\');
        $result = mysql_query($sql);

    for($x=0;$x<$fields;$x++){ // start for loop

    $file_name = $_FILES[\'image\'. $x][\'name\'];
    $uploads = \'upload\'; // directory to upload to
    $copy = copy($_FILES[\'image\'. $x][\'tmp_name\'],$uploads.\'/\'.$file_name);

        if($copy) { // check if successfuly copied
$file = \'image$x\';
            $sql = \"
       UPDATE `test`
       SET `$file` = \'$file_name\'
       WHERE `id` = \'$num\'
     \";
            $result = mysql_query($sql); // run SQL query against DB
            echo \"$file_name | uploaded successfuly!<br>\\n\";

        }else{
            echo \"$file_name | could not be uploaded!<br>\\n\";
        }

    } // end for loop

} // end of submit statement


?>

</body>
</html>

try something like that, i havnt tested it and its just off the top of my head really, but its a basic idea.. or something:\\
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 2 Guest(s)