在 html 上传表单中检查 php 数组值

  • 本文关键字:php 数组 html 表单 php
  • 更新时间 :
  • 英文 :


我可以检查一个简单的变量是否为空。但是我如何检查来自 HTML 上传表单的数组值。比如:名称=图像[];

简单的变量检查:

if(empty($somevalue))
  $err[] = "your variable is empty";

但是我如何检查它是否是这样的:

<input name="images[]" type="file"/>

更新代码:

<?php
if(isset($_POST['Submit']) && $_POST['Submit'] == "Save & Continue")
{
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
$img[] = $_FILES['images']['name']; 
$img = array();
$err = array();
if(isset($img))
{
    if(empty($img)) 
        $err[] = "Your property picture require";           
}
if(!empty($err))
{
    foreach($err as $er)
    {
        echo $er;
    }   
}
else
{
    echo "done";    
}
}
?>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data"   
name="mutiple_file_upload_form" id="mutiple_file_upload_form">
<table width="1020" border="0" cellspacing="7" cellpadding="0">   
 <tr>
 <td valign="top" width="250">
     Upload your property pictures
     </td>
     <td>
      <div id="file_container">
        <input name="images[]" type="file" class="tr2"  />
        <br />
      </div>
      <a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
      <input type="submit" value="Save & Continue" name="Submit" class="submit" />
      </td>
 </tr> 
 </table>
</form> 
</table>  

仍然可以在数组上使用empty()

从文档中:

以下内容被视为空:


array() (一个空数组)
...

但是,您应该注意,对于包含元素的数组,empty()将返回false。如果您需要考虑这一点,请查看array_filter()

你只需忽略 PHP 端的方形背板

if empty($_POST['images'])