views:

92

answers:

5

The Problem:


Hi! I'm making a file upload form where I can upload photos, I added multiple="" to the input and for the name="upload_photo[]" so I'm able to upload multiple files. When I print $_FILES['upload_photo'] I get this and I want to get the values of each key with foreach.

So for example I want to get just [name] what will be egg.jpg and green.jpg.

Array
(
    [name] => Array
        (
            [0] => egg.jpg
            [1] => green.jpg
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => image/jpeg
        )

    [tmp_name] => Array
        (
            [0] => C:\wamp\tmp\php50E9.tmp
            [1] => C:\wamp\tmp\php50EA.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 24450
            [1] => 65030
        )

)

This is what I have tried so far


But this gives me everything I don't have control over it.

foreach($_FILES['upload_photo'] as $keys => $file){

  foreach($_FILES['upload_photo'][$keys] as $key => $files){
   echo $files . "<br />";
  } 
 }
+1  A: 

I think this is what you're trying to do, give this a shot:

foreach($_FILES['upload_photo'] as $photo) {
    echo $photo['name'];
}

I don't know how your $_FILES array got structured like this, and it appears you changed your question since i first answered, as my solution worked for the way your question was originally posted.

So for the new array structure of $_FILES you'll have to use nested loops, like so:

foreach($_FILES['upload_photo'] as $photo) {
    foreach($photo['name'] as $name) {
        echo $name;
    }
}
jordanstephens
The result : `Notice: Undefined index: name in C:\wamp\www\includes\upload_photo.php on line 7` x5 , x5 because there are 5 keys.
CIRK
@CIRK see updated solution
jordanstephens
I didn't changed my question, I just improved it to be more understandable :), I think this will work, I've made something similar right now, let me try it.
CIRK
you changed the structure of the `$_FILES` array, which changes the question: http://stackoverflow.com/posts/3418628/revisions
jordanstephens
`I added multiple="" to the input and for the name="upload_photo[]" so I'm able to upload multiple files.` that was there in the original question too, just because I provided other values I didn't changed the structure of the array.
CIRK
And I get an error what says that the index `name` doesn't exists , with your code.
CIRK
This shouldn't work. When there are multiple files with the same name, `$_FILES` doesn't break `$_FILES["whatever"]` into an array of files, it makes the keys of it into arrays. So for instance, you wouldn't use `$_FILES["whatever"][0]["name"]`, you'd use `$_FILES["whatever"]["name"][0]`.
mattbasta
A: 

Sounds like $_FILES is weird. So to get the names, what you want is simply...

foreach($_FILES['upload_photo']['name'] AS $name) {
    echo $name . "<br />";
}

Or, based on my comment below, you could do...

foreach($_FILES['upload_photo'] AS $key => $file) {
    if ($key == 'name') {
        foreach($file AS $name) {
          echo $name . "<br />";
        }
    }
}
Andrew
@Andrew, but then I need to make a foreach for all of the keys! Are you sure that this is the best way? :S
CIRK
Based on how the $_FILES array is designed, it appears so - you have to go through each key to get the information for each file, except (as would make more sense) if it were designed like `$_FILES['upload_photo'][0]['name']`. The only other way, really, would be to have a single loop like `foreach($_FILES['upload_photo'] AS $key => $file)`, and then check `if $key = "name"` in each iteration. Which, actually, if you combined it with a `switch` statement, might not be a bad way of going.
Andrew
Also, in your question, you specified wanting just the `name`, so don't now turn around and say "but that only gets me the name!' ;)
Andrew
+1  A: 

Is this what you're after?

foreach($_FILES['upload_photo'] as $key => $value)
{
   for ($i = 0; $i < count($_FILES['upload_photo'][$key]); $i++)
   {
      echo $_FILES['upload_photo'][$key][$i];
   }

}

Not sure this is how I'd want to handle this situation, but I think it would work in your case.

edit: forgot the count

edit2: If you know that your array contains the keys shown in the sample above, to receive all the values from just the ['name'] array, you'd do the follow:

for ($i = 0; $i < count($_FILES['upload_photo']['name']); $i++)
{
   $name = $_FILES['upload_photo']['name'][$i];
   //do something else
}

OR

foreach ($_FILES['upload_photo']['name'] as $value)
{
   $name = $value;
   //do something else
}

As others have shown -

You seem to know the array keys involved / they are static, so the only thing left to do is figure out how many files there are - count the items inside the array and loop through it that many times.

kmfk
+1  A: 

maybe this is the idea to help you when executing the following code with your given data in $_FILES it will print something like

upload 1

name = ...

size = ...


upload 2

name = ...

size = ...

$numUploads = count($_FILES['upload_photo']['name']);
for ($n = 0; $n < $numUploads; $n++) {

    echo 'upload ' . $n . '<br/>';
    foreach ($_FILES['upload_photo'] as $prop => $values) {

        echo $prop . ' = ' . $_FILES['upload_photo'][$prop][$n] . '<br/>';
    }

    echo '<hr/>';
}
zolex
Thanks for your answer, let me try it.
CIRK
Hmm, and how should I get just the `name` or `tmp_name`? I need to specify a variable for them.
CIRK
remove the inner loop and do something like$currentFileName = $_FILES['upload_photo']['name'][$n];$currentTempName = $_FILES['upload_photo']['tmp_name'][$n];
zolex
+1  A: 

If all you want is JUST the names of the uploaded files, then this will work:

foreach($_FILES['upload_photo']['name'] as $idx => $name) {
    echo "File #$idx: $name<br />"
}

If you're wanting to process each uploaded file, then you'd need something like this:

foreach(array_keys($_FILES['upload_photo']['name']) as $idx) {
   move_uploaded_file($_FILES['upload_photo']['tmp_name'][$idx], '/some/where/on/your/server');
}
Marc B