Uploading multiple files-opinion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jankie
    New Member
    • May 2007
    • 58

    Uploading multiple files-opinion

    Hi everybody !
    I'd be grateful if anyone can point out to me what is missing to get following code working

    [code=php]<?php
    $filename = $_FILES['userfile']['name'];
    $tempname = $_FILES['userfile']['tmp_name'];
    ------
    -------
    ?>

    <form action="file-upload.php" method="post" enctype="multip art/form-data">
    Send these files:<br />
    <input name="userfile1 " type="file" /><br />
    <input name="userfile2 " type="file" /><br />
    <input type="submit" value="Submit" />
    </form>

    foreach ($_FILES as $file) {

    if (move_uploaded_ file($_FILES['userfile']['tmp_name'][$file], $uploadfile)) {

    ..........
    ..........

    ... }[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    I also wonder if this would be enough to insert multiple files at once in a db.

    My gratitude in advance !
    Last edited by pbmods; Jul 8 '07, 09:55 PM. Reason: Added CODE Tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Jankie.

    Your inputs are going to populate $_FILES['userfile1'] and $_FILES['userfile2'].

    If in doubt:[code=php]print_r($_FILES );[/code]

    Alternatively, if you want to allow for the possibility of more than 2 files, you can name your inputs 'userfile[]', which will create $_FILES['userfile'][0], $_FILES['userfile'][1], $_FILES['userfile'][2], etc.

    E.g.:[code=html]
    <input name="userfile[]" type="file" /><br />
    <input name="userfile[]" type="file" /><br />
    <input name="userfile[]" type="file" /><br />
    .
    .
    .
    <input type="button" onclick="create FileInput();" value="Add Another" />
    [/code]

    Comment

    • Jankie
      New Member
      • May 2007
      • 58

      #3
      Thank you Pbmods for your readiness to offer a hand(as always :)
      Yes,print_r positive
      Now if you can afford a bit more kindness trespassing,whi ch do you think is the best(or the correct) way of finishing it up this multiple uploads ?

      [code=php]foreach ($_FILES["userfile"]["error"] as $file) {
      if (move_uploaded_ file($file['userfile']['tmp_name'][$file], $uploadfile)) {
      ....
      }[/code]

      or

      [code=php]foreach ($_FILES["userfile"]["error"] as $file) {
      move_uploaded_f ile($file["userfile"]["tmp_name"],$file["userfile"]["name"],$uploadfile)
      ..
      }[/code]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Jankie.

        What you'll want to do is iterate through $_FILES itself:
        [code=php]foreach($_FILES as $array)[/code]

        Since $_FILES is going to look like this:
        [code=php]
        $_FILES = array(
        'userfile1' => array( // <-- We want to work with this array first...
        'name' => 'apicture.jpg',
        'type' => 'image/jpeg',
        'size' => '45000',
        'tmp_name' => '/tmp/php_123456abcd' ,
        'error' => 0
        ),
        'userfile2' => array( // ... then this one.
        'name' => 'anotherpicture .jpg',
        'type' => 'image/jpeg',
        'size' => '48000',
        'tmp_name' => '/tmp/php_dcba_654321 ',
        'error' => 0
        )
        );
        [/code]

        Probably what you'll want to do first is make sure that each file was uploaded correctly:
        [code=php]
        $errors = array();
        foreach($_FILES as $key => $array)
        if($array['error'] !== UPLOAD_ERR_OK)
        $errors[] = "$key had error {$array['error']}!";

        if(! empty($errors))
        exit(implode('< br />', $errors));
        [/code]

        Once you've determined that there were no errors, then you can process your files:
        [code=php]
        foreach($_FILES as $key => $array) {
        if(! move_uploaded_f ile( $array['tmp_name'], $array['name'] ))
        $errors[] = "$key could not be moved!";
        }

        if(! empty($errors))
        exit(implode('< br />', $errors));
        [/code]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

          You will still need to use [/CODE] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

          [CODE=html]
          [CODE=javascript]
          [CODE=php]

          and so on.

          Thanks!

          MODERATOR

          Comment

          • Jankie
            New Member
            • May 2007
            • 58

            #6
            Thanks a lot Pbmods
            Interesting the code thing,never heard of it as a closing tag
            Time to sleep for the day
            Happy weekdays ahead !

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Jankie.

              Originally posted by Jankie
              Interesting the code thing,never heard of it as a closing tag
              Yeah, it can be a little confusing. For more info, check out the 'REPLY GUIDELINES' box on the right side of the page when you post.

              Comment

              Working...