0

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 :

  • Detect if a file is being uploaded
  • If yes, store the path of the file inside $v so that I can use that array in the same manner as specified above

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

}
asprin
  • 9,579
  • 12
  • 66
  • 119
  • I don't see a reason to downvote? :S – 1321941 Nov 02 '12 at 13:24
  • @PatrickJamesMcDougle I didn't try because I'm not able to figure out the way for proceeding ahead. `$v = $_POST[v]` will store all values from textboxes, select tags, textareas etc., but how to work with arrays when dealing with file uploads? – asprin Nov 02 '12 at 13:27
  • 1
    @MrD I've kind of gotten accustomed to those now. The people who downvote don't even have the courtesy of posting the reason. – asprin Nov 02 '12 at 13:28
  • 1
    You really, really need to guard against SQL injection: http://stackoverflow.com/a/681600/246847 – Tom Smilack Nov 02 '12 at 13:29
  • @TomSmilack I'm aware of that. But right now my priority is to find a solution. The next step would be looking at security – asprin Nov 02 '12 at 13:30
  • Let me direct you to the php documentation: http://www.php.net/manual/en/features.file-upload.post-method.php – Patrick James McDougle Nov 02 '12 at 13:21
  • Sorry about the hasty downvote. I thought you didn't know how to deal with uploaded files and the question didn't show research effort. I have removed the downvote. – Patrick James McDougle Nov 02 '12 at 14:13
  • @PatrickJamesMcDougle Appreciate it. I should have rephrased the scenario in a better way – asprin Nov 02 '12 at 14:15
  • @asprin I know! Do you have access to the form that is being posted? – 1321941 Nov 02 '12 at 17:55

1 Answers1

1

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

  • Again, I'm well aware of handling file uploads. What I'm looking for is what to put inside the `if` condition. Once I get inside that condition, I can use `$_FILES` to complete the upload process – asprin Nov 02 '12 at 14:20
  • That makes sense. But I tested it with a form. I kept `` in a form and used `if(!empty($_FILES)){echo 'file present'; }else{echo 'file not present'; }`. Then I just hit the submit button without choosing a file. I got `file present` printed out which is wrong. – asprin Nov 05 '12 at 06:01
  • Ok. Using `print_r($_FILES)` brought up an array with error code as `4`. Any way to get around this? – asprin Nov 05 '12 at 06:04
  • Never mind, I figured it out. You pointed me in the right direction. I'm first checking for `if($_FILES)` and inside this I'm looping through `error` and checking `if(error>0)`. On each loop I'm again checking `if(error == 4)` which would mean no file was selected to upload. In the else part I'm handling the error. And finally `if(error ==0)`, I'm handling the upload process. – asprin Nov 05 '12 at 07:08