0

I have a html form sending to php script with "post" method (no ajax involved).

In the form i have some type="file" fields.

Now in my php script i have a php foreach loop looping through the sent files:

foreach($_FILES as $file) {
// Some checks and actions.
}

In that loop i want to be able to use the current file array name (which is equal to the name attribute of the upload filled it was uploaded from of course), so i treid:

foreach($_FILES as $file) {
// Some checks and actions.
print ($file);
}

But that's just echoing "Array".

Important note: My $_FILES array is structured like that:

Array
(
    [_pf_photo1] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [_pf_photo2] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [_pf_photo3] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [_pf_photo4] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [_pf_photo5] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [selfimage] => Array
        (
            [name] => IMG_9785.JPG
            [type] => image/jpeg
            [tmp_name] => /tmp/phpWMOKhn
            [error] => 0
            [size] => 104221
        )

)

($_FILES can be structured another way, so to make things clear). so in that example what i want to get in the loop (for the array name part) is:

_pf_photo1, _pf_photo2, _pf_photo3, _pf_photo4, _pf_photo5, selfimage

7
  • use print_r or var_dump for debugging. print works only with strings. Commented Jul 18, 2017 at 17:17
  • foreach($_FILES as $key => $file) { echo $key; } Commented Jul 18, 2017 at 17:18
  • Share form code Commented Jul 18, 2017 at 17:23
  • That is already suggested in the @George shaw's answer. see comment there. Commented Jul 18, 2017 at 17:23
  • Will sharing the form code will be helpful even thought the print_r($_FILES) is shared? Commented Jul 18, 2017 at 17:25

2 Answers 2

4

This may be what you're looking for if I am understanding your problem correctly.

foreach($_FILES as $key => $value) {
    echo $key;
}

The only other way I can understand this question is if you're incorrectly using print. To print an object/array out with all of its data you can use var_dump instead of print.

Sign up to request clarification or add additional context in comments.

3 Comments

it guess it will work but then i won't be able to use things like $file[name] in the middle of the loop. am i right?
You will definitely be able to access array values inside of that loop. In my example it would look like this echo $value['name']; because $value references an array within $_FILES whereas $key just references the name of the array.
Great! Thanks! finally got that (sort of...:)
2

I hope you are looking to extract your keys

foreach ($_FILES as $key => $file) { 
  echo $key; // print your array key
  echo $file['name']; // print your name inside that array
} 

1 Comment

Thanks, i have seen the other one first (i guess that has to do with the way Stackoverflow works) and add some comments so i accepted it, but i did up vote you also. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.