asprin
asprin

Reputation: 9833

Determine if a file is being submitted through a form while dealing with arrays

First of all sorry for the confusing thread title. I couldn't come up with a better one.

I use associative arrays for names of form elements so that it makes it easier to run the insert query while processing the form. Something like:

<input type="text" name="v[fname]" />
<select name="v[location]">
    <option val="1">ABC</option>
    <option val="2">DEF</option>
</select>
<textarea name="v[comments]"></textarea>

So that I could simply do:

$v = $_POST[v];

// single line execution for insert    
"INSERT INTO ".$tableName." ( ". implode( ',', array_keys( $v) ) .") VALUES( '". implode( "','", $v ) . "')"

Now sometimes, the forms that I deal with are bound to contain <input type="file" /> elements. I was wondering if there is a way to :

So, in a nutshell, I'm looking for something like this:

if(isset($_POST['add'])) // when submit button is clicked
{
   $v = $_POST['v']; // store other element values
   if(condition to check if a file is being uploaded through the form)
   {
      $path = 'get the path where it will be uploaded'; //This part I can handle. What I'm having trouble with is finding a way to get into **this** if condition
      $v['path'] = $path; // store the path inside $v
   }

   //proceed with the insert statement as usual

}

Upvotes: 0

Views: 107

Answers (1)

Patrick James McDougle
Patrick James McDougle

Reputation: 2062

Can you check the contents of $_FILES?

if(!empty($_FILES)) {
  //do what you need to do
}

doc: http://www.php.net/manual/en/reserved.variables.files.php

Upvotes: 1

Related Questions