SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple uploads difficulty

    Greetings to everyone !
    I hope someone can offer a hand getting the php code to work for a multiple upload as the one below:

    <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>

    I googled and found this piece of code:

    <?php
    foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
    echo"$error_codes[$error]";
    move_uploaded_file(
    $_FILES["pictures"]["tmp_name"][$key],
    $_FILES["pictures"]["name"][$key]
    ) or die("Problems with upload");
    }
    }
    ?>

    Problem is am not interested in the error portion of this code.I'd like to strip it like this(but still get the code to work)

    move_uploaded_file(
    $_FILES["pictures"]["tmp_name"][$key],
    $_FILES["pictures"]["name"][$key]
    ) or die("Problems with upload");

    Since the $key stands for the $error(that I dont need),I dont know what to replace it for

    Thank you very much in advance for your support !

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will work fine:

    PHP Code:
    <?php
    foreach ($_FILES["pictures"]["error"] as $key => $error) {
    move_uploaded_file(
    $_FILES["pictures"]["tmp_name"][$key],
    $_FILES["pictures"]["name"][$key]
    ) or die(
    "Problems with upload");
    }
    ?>
    just forget about $error variable.

    Alternatively you can do the following:
    PHP Code:
    <?php
    foreach ($_FILES["pictures"]["error"] as $file) {
    move_uploaded_file($file["pictures"]["tmp_name"],$file["pictures"]["name"]) or die("Problems with upload");
    }
    ?>

  3. #3
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much sergiy!
    Just one more opinion if you're still on
    Would it affect anything if i remove the $file["pictures"]["name"] portion and just keep the :
    move_uploaded_file($file["pictures"]["tmp_name"],$filepath); ?

    I chose your last option as I already handle upload errors in a different way.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, sure you can do that.

  5. #5
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you again Sergiy for your help.
    Anyone can share how to get this working for a db as well? (multiple uploads to a db)

    So far with Sergiy's rectified code i can only have one file stored in db out of 3.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •