How can I have PHP identify $POST values.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • recordlovelife
    New Member
    • Sep 2007
    • 31

    How can I have PHP identify $POST values.

    I have a photo uploader for a client. It's a simple input file button.

    But they wanted to be able to add more than one picture.
    So, when the client clicks the "add another picture?" button, I used javascript to clone the initial input button, and then used a counter to increase a digit in the name. I.E:

    First input buttons name => name="uploaded1 "
    When Client Wants Another pick => name="uploaded2 "
    ...Another One => name="uploaded3 "
    (and so on)


    now on my php side, im using this to move the file into a folder on the server and save the path in a MySQL table.

    [PHP]
    $target = "wherepixgo/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $pictureurl = $target;
    if(move_uploade d_file($_FILES['uploaded']['tmp_name'], $target))
    {
    if($target=="wh erepixgo/"){
    $target="";
    echo '<center><br /><br /><br />';
    echo "No file Uploaded.";
    echo '</center>';
    echo '<br><br><br>' ;
    }
    else {
    echo '<center>';
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    echo '<br><br><br></center>';
    }
    }[/PHP]

    That's great for when I only had one picture to upload, and it would just look for one input file button name "uploaded". Now there are going to be input files to the Nth degree. How can I tell php to look at all of the posted variables named "uploaded(# )" and loop through an inputting code til no more.

    Psuedocode:

    [PHP]
    while(there are posted variables with the name "uploaded#" ){
    find it's # at the end of its name;
    upload the current photo;
    copy the path name into a variable so i can save it in MySQL;
    }
    [/PHP]
    now i figure the only tricky thing about this would be searching through the post data and selecting everytime it sees the world "uploaded".

    A little help, ps...if i am missing a very obvious and common feature of php, please tell me as im still learning (for life).

    thanks in advance.
  • steven
    New Member
    • Sep 2006
    • 143

    #2
    I believe, instead of changing the name by incrementing a counter each time, you could simply make the name `uploaded[]` and it will be an array. You would not need to change the name at all and instead, could simply pull the entire array.

    So, for example <input type="file" name="images[]" /> and $_FILES[images] would be an array of multiple files.

    I'm pretty certain it works that way.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      What Steven suggested should work. It does work on normal POST elements so I see no reason why it shouldn't work here.

      In case it doesn't, however, you could loop through the $_FILES array with a foreach loop:
      [code=php]
      foreach($_FILES as $file) {
      if($file['error'] == 0) {
      # ...
      }
      }
      [/code]
      You may want to add some extra validation tho, in case there are files you don't want mixed in there.

      Comment

      • recordlovelife
        New Member
        • Sep 2007
        • 31

        #4
        ok, so i looked into what you two were both saying, but i cant seem to get it to work. I understand that if i simply call the name of the file input "uploaded[]", it will enter the file names into and array.

        so this is what is printing the form for me: [PHP]
        $max_no_img=5;

        echo "<form method=post action=postnews .php enctype='multip art/form-data'>";
        echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
        for($i=1; $i<=$max_no_img ; $i++){
        echo "<tr><td>Im ages $i</td><td>
        <input type=file name='uploaded[]' class='bginput' ></td></tr>";
        }

        echo "<tr><td colspan=2 align=center><i nput type=submit value='Submit'> </td></tr>";
        echo "</form> </table>";
        [/PHP]


        so that will make me 5 input files.


        Now my posting side wont work. And I want to be able to understand why it isn't.

        [PHP]
        {
        foreach($_FILES['uploaded']['name'] as $value)
        {
        if(!empty($valu e){
        $target = "uploadedpictur es/";
        $target = $target . $value ;
        if(move_uploade d_file($_FILES['uploaded']['tmp_name'], $target)){
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
        echo '<br>';
        }
        }

        }

        }[/PHP]


        It doesn't work, i get this error

        Notice: Array to string conversion in postnews.php on line 13

        The only array to string i can see is maybe using foreach to turn an array element into "$value"? Please help, this has been like a 2 day struggle for me and the client.

        Thanks

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          When you POST it like that, the $_FILES['uploaded'] is an array of files. It looks more like this:
          [code=php]
          $_FILES['uploaded'] = array(
          array(
          "name" => "name1",
          "tmp_name" => "tmp_path1" ,
          // etc...
          )
          array(
          "name" => "name2",
          "tmp_name" => "tmp_path2" ,
          // etc...
          )
          array(
          "name" => "name3",
          "tmp_name" => "tmp_path3" ,
          // etc...
          )
          )
          [/code]


          You need to do something more like this:
          [code=php]
          foreach($_FILES['uploaded'] as $_v)
          {
          $fileName = $_v['name'];
          $tmpName = $_v['tmp_name'];
          # etc...
          }
          [/code]

          Comment

          • steven
            New Member
            • Sep 2006
            • 143

            #6
            Just a final tip... When working with arrays, print_r and <pre> are your friends.

            Code:
            echo "<pre>".print_r($array, 1)."</pre>";

            Comment

            • recordlovelife
              New Member
              • Sep 2007
              • 31

              #7
              thanks to the both of you, this makes alot of sense now.

              Comment

              Working...