Jump to content

Archived

This topic is now archived and is closed to further replies.

feri_soft

Multiple upload with db support

Recommended Posts

Hi,
I need a multiple file upload script that have for example 5 fields and can only allow gif and jpg to be uploaded.Another very important thing is to have db support so the data of the files can be stored in db data such as... filename,size and file_id...

I created a solution for this but only for single file upload...but for multiple!?

Thank you!


p.s

I found this on the web but i cant figure it out how to create a db support for it:
[code]<?php
/* Handle multiple file upload easily */
/* by Ronny Haryanto <giant@canada.com> */
/* February-ish 1999 */

/* Use it however you want. I'm not responsible for what you do with it. */

/* how many upload slots? */
define("UPLOAD_SLOTS", 5);

/* where to move the uploaded file(s) to? */
define("INCOMING", "/home/ronny/incoming/");

if($REQUEST_METHOD!="POST")
{
        /* generate form */
        echo "<form enctype=\"multipart/form-data\" method=post>\n";
        for($i=1; $i<=UPLOAD_SLOTS; $i++)
        {
                echo "<input type=file name=infile$i><br>\n";
        }
        echo "<input type=submit value=upload></form>\n";
}
else
{
        /* handle uploads */
        $noinput = true;
        for($i=1; $noinput && ($i<=UPLOAD_SLOTS); $i++)
        {
                if(${"infile".$i}!="none") $noinput=false;
        }
        if($noinput)
        {
                echo "error uploading. create 150MB coredump instead?";
                exit();
        }
        for($i=1; $i<=UPLOAD_SLOTS; $i++)
        {
                if(${"infile".$i}!="none" &&
                copy(${"infile".$i}, INCOMING.${"infile".$i."_name"}) &&
                unlink(${"infile".$i}))
                {
                        echo ${"infile".$i."_name"}." uploaded<br>";
                }
        }
} /* else */
?>[/code]

Share this post


Link to post
Share on other sites
on the form, have
<input type='file' name='infile[]' />

This makes them an array

then loop through the array

Share this post


Link to post
Share on other sites
go to:

http://us3.php.net/manual/en/features.file-upload.php

has an example of uploading an array of files.

Share this post


Link to post
Share on other sites
Ok i found this but how to create db support for it:

<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
  if ($error == UPLOAD_ERR_OK) {
      $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
      $name = $_FILES["pictures"]["name"][$key];
      move_uploaded_file($tmp_name, "data/$name");
  }
}
?>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.