0

I have a php form that has a known number of columns (ex. top product, price, quantity, file(file input)), but has an unknown number of rows, as users can add rows as they need.

I am using a multidimensional array:

name="prod[0][name]"
name="prod[0][qty]"
name="prod[0][file]"

on post I use

 if ( isset( $_POST['prod'] ) )
    {

        foreach ( $_POST['prod'] as $value )
        {
            //some code here
if (isset($_FILES[$value["file"]]["name"]) && 
    ($_FILES[$value["file"]]["size"] > 0) &&
    ($_FILES[$value["file"]]["error"]==0) &&
    $_FILES[$value["file"]]["name"] != "" )
  {
   //code of uploading file

 } 

    }

My problem is I can't get the file to be uploaded, I always get the error

$value["file"] is undefined

please help

2 Answers 2

1

The error correctly states that there is no file key in your array at that level. Instead you should use the key from the foreach loop.

On the top of my head, it should be something like:

foreach ( $_POST['prod'] as $key => $value ) {
                            ^^^^ this will be your numeric index
    //some code here
    if (isset($_FILES['prod']['name'][$key]['file']) ...

But you'd better do a var_dump($_FILES['prod']); to confirm that as I am not sure how php handles multi-level file upload arrays.

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

Comments

0

Have you set the enctype attribute on the form element ?

<form ... enctype="multipart/form-data">
...
</form>

http://www.w3schools.com/tags/att_form_enctype.asp

Comments

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.