multi-file upload - $_POST not working...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    multi-file upload - $_POST not working...

    Hi everybody!
    I have a multi-file upload form... Please looks at my code below. It's
    in an initial stage. I'm trying to display the values of each text box
    when I press "upload" but it doesn't seem to work... i don't know
    what's the problem.. I'm using PHP5.

    <html>
    <head>
    </head>
    <body>
    <?php
    function ifcond($i) {
    if ($i > 9) {
    return $i;
    }
    else {
    return '0'.$i;
    }
    } //end function ifcond()
    for ($i=1; $i < 11; $i++) {
    $image[$i - 1] = $_POST[`echo 'image'.ifcond( $i);`];
    }
    print_r($image) ;
    ?>

    <form action="<?php echo $_SERVER['../PHP_SELF']; ?>"
    method="post">
    <?php
    for ($i=1; $i < 11; $i++) { ?>
    Image<?php echo ifcond($i); ?>:
    <input type="file" name="image"<?p hp echo ifcond($i); ?> /><br /><br
    />
    <?php } ?>
    <input type="submit" value="Upload" /></p></form>
    </form>
    </body>
    </html>

    Thanx!
    Ben
  • Alvaro G Vicario

    #2
    Re: multi-file upload - $_POST not working...

    *** Ben wrote/escribió (22 Sep 2004 23:53:06 -0700):[color=blue]
    > I have a multi-file upload form... Please looks at my code below. It's
    > in an initial stage. I'm trying to display the values of each text box
    > when I press "upload" but it doesn't seem to work... i don't know
    > what's the problem.. I'm using PHP5.[/color]

    I can't figure out what this code is supposed to do, I'd bet that it
    doesn't even output valid HTML:

    <input type="file" name="image"1 /><br /><br />
    <input type="file" name="image"2 /><br /><br />
    <input type="file" name="image"3 /><br /><br />
    <input type="file" name="image"4 /><br /><br />
    <input type="file" name="image"5 /><br /><br />



    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- Thank you for not e-mailing me your questions
    --

    Comment

    • coolsti

      #3
      Re: multi-file upload - $_POST not working...

      On Wed, 22 Sep 2004 23:53:06 -0700, Ben wrote:
      [color=blue]
      > Hi everybody!
      > I have a multi-file upload form... Please looks at my code below. It's
      > in an initial stage. I'm trying to display the values of each text box
      > when I press "upload" but it doesn't seem to work... i don't know
      > what's the problem.. I'm using PHP5.[/color]


      I haven't studied this in detail enough to know exactly where your problem
      is, but I can see one problem right away. You have one misplaced quotation
      mark, namely the one after image on this line:
      [color=blue]
      > <input type="file" name="image"<?p hp echo ifcond($i); ?> /><br /><br[/color]

      I believe you want the return from function ifcond to be attached to the
      word image, not come after it in the input tag.

      But more importantly, you should try to rewrite your code so that it is
      more readable. I very rarely mix clear html text with php tags for the
      very reason that your piece of code here is very hard to debug. There are
      many ways to write this piece of code, some more efficient than others,
      but forgetting efficiency for the moment (php is fast anyway), this might
      read better instead of the equivalent lines that you wrote:

      <?php
      for ($i=1; $i < 11; $i++)
      {
      $str = ifcond($i);
      echo "Image$str:<inp ut type=\"file\"
      name = \"image$str\ " /><br /><br />";
      }
      ?>

      Granted, it is clumsy having to use \" within to write a quote within the
      echo command, but it reads better than having to parse a handful of php
      tags by eyeball. Note here also, you have only one call to your function
      instead of two. Also, if you are using a php-syntax aware editor,
      something like Image$str looks very readable since the $str will be taken
      as a PHP variable and made in a different color than Image.

      Regards,
      Steve, Denmark

      Comment

      • coolsti

        #4
        Re: multi-file upload - $_POST not working...

        As an afterthought to my reply, even better would be to use the following
        construct, making your function ifcond unnecessary. If you get used to
        the " if () ? true : false " syntax, things get a lot easier:

        [color=blue]
        > <?php
        > for ($i=1; $i < 11; $i++)
        > {
        > $str = ($i < 10) ? "0$i" : $i;
        > echo "Image$str:<inp ut type=\"file\"
        > name = \"image$str\ " /><br /><br />";
        > }
        > ?>
        >[/color]

        Regards,
        Steve, Denmark

        Comment

        • Ben

          #5
          Re: multi-file upload - $_POST not working...

          coolsti <coo@goontrytos pamme.com> wrote in message news:<pan.2004. 09.23.12.29.51. 422000@goontryt ospamme.com>...[color=blue]
          > As an afterthought to my reply, even better would be to use the following
          > construct, making your function ifcond unnecessary. If you get used to
          > the " if () ? true : false " syntax, things get a lot easier:
          >[/color]
          <snip>

          thanx for tips steve..

          i managed to find the problem... Have a look at the code below... The
          reason "print_r($image );" doesn't show the selected items is
          something to do with enctype="multip art/form-data". If I remove the
          "enctype=.. ..", "print_r($image );" displays the selected items.. But I
          don't know the real relationship behind $_POST and enctype. Maybe
          someone can explain me... I think the easier and a better way is to
          use $_FILES with an array for "name" as in: <input type="file"
          name="image[]">


          <html>
          <head>
          </head>
          <body>
          <?php
          for ($i=1; $i < 11; $i++) {
          $str = ($i < 10) ? "0$i" : $i;
          $image[$i - 1] = $_POST["image".$st r];
          }
          print_r($image) ;
          echo "<br />";
          print_r($_FILES );

          ?>

          <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
          enctype="multip art/form-data" method="post">
          <?php
          for ($i=1; $i < 11; $i++) {
          $str = ($i < 10) ? "0$i" : $i;
          echo "Image$str:<inp ut type=\"file\" name=\"image$st r\" /><br
          /><br/>";
          } ?>
          <input type="submit" value="Upload" /></p></form>
          </form>
          </body>
          </html>

          Ben

          Comment

          • cool

            #6
            Re: multi-file upload - $_POST not working...

            [color=blue]
            > thanx for tips steve..[/color]

            Have you read the PHP documentation (google search for "php documentation"
            and take the first link) on file uploads? This explains pretty much how
            things work, and yes, I believe when you do a file upload, your file names
            are found in the $_FILES array. Note that your files are stored on your
            server while the script is running with php-given temperary names in some
            temperary directory, but you can access all this information using
            $_FILES, see documentation. You need to do something with your uploaded
            file before your upload script ends, e.g. move the temporary uploaded
            files to more permanent locations, or they will disappear.

            Regards,
            Steve

            Comment

            • Shawn Wilson

              #7
              Re: multi-file upload - $_POST not working...

              > > I have a multi-file upload form... Please looks at my code below. It's[color=blue][color=green]
              > > in an initial stage. I'm trying to display the values of each text box
              > > when I press "upload" but it doesn't seem to work... i don't know
              > > what's the problem.. I'm using PHP5.[/color]
              >
              > I haven't studied this in detail enough to know exactly where your problem
              > is, but I can see one problem right away. You have one misplaced quotation
              > mark, namely the one after image on this line:
              >[color=green]
              > > <input type="file" name="image"<?p hp echo ifcond($i); ?> /><br /><br[/color][/color]

              Sorry to reply to this message, but I'm unable to view the original message.

              You can actually do this:
              <input type = "file" name = "userfile[]" />
              <input type = "file" name = "userfile[]" />
              <input type = "file" name = "userfile[]" />
              <input type = "file" name = "userfile[]" />
              <input type = "file" name = "userfile[]" />

              and in your PHP code do this:

              foreach($_FILES['userfile']['name'] as $key=>$val) {
              echo "NAME OF FILE ".$key.": ".htmlentities( $val)."<br />\n";
              echo "TMPNAME OF FILE ".$key.":
              ".htmlentities( $_FILES['userfile']['tmp_name'][$key])."<br /><br />\n\n";
              //do your validation and stuff
              }

              And that way you don't have to deal with missing files (like if there are 10
              fields and the user selects files in 1,2,3,6,7 and 8.

              Shawn
              --
              Shawn Wilson
              shawn@glassgian t.com

              Comment

              Working...