0

首先对令人困惑的主题标题感到抱歉。我想不出更好的了。

我使用关联数组作为表单元素的名称,以便在处理表单时更轻松地运行插入查询。就像是:

<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>

这样我就可以简单地做:

$v = $_POST[v];

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

现在有时,我处理的表单必然包含<input type="file" />元素。我想知道是否有办法:

  • 检测文件是否正在上传
  • 如果是,则将文件的路径存储在其中,$v以便我可以以与上面指定的相同方式使用该数组

所以,简而言之,我正在寻找这样的东西:

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

}
4

1 回答 1

1

你能检查 $_FILES 的内容吗?

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

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

于 2012-11-02T14:17:59.180 回答