验证php中的多选字段

时间:2014-01-30 05:25:23

标签: javascript php jquery

我有一个带有多选文件字段的表单。现在我想在点击提交按钮时检查文件是否是selecetd。目前我正在使用以下代码,

PHP代码: -

<input type="file" id="files" name="files[]"  multiple="multiple"  />

<?php 


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

    if((isset($_FILES['files'])) && ($_FILES['files']['size'] > 0))
    {
        echo "Files is seleced";
    }
    else
    {
        echo "Please select file";
    }
}

?>

但点击提交按钮后,如果没有选择图像,那么它也显示文件被选中meassage.so请给出一些解决方案。

1 个答案:

答案 0 :(得分:0)

$_FILES['files']multidimensional数组

所以你必须检查你的状况

$sizes = $_FILES['files']['size'];

$sizes = array_filter($sizes); //remove all 0 values 

if((isset($_FILES['files'])) && (count($sizes) > 0))

<强>例如

Array
(
    [name] => Array
        (
            [0] => chtbg.jpg
            [1] => chtlg.jpg
            [2] => ddsports_bg.jpg
            [3] => ddsports_logo.jpg
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => image/jpeg
            [2] => image/jpeg
            [3] => image/jpeg
        )

    [tmp_name] => Array
        (
            [0] => E:\xampp\tmp\php849B.tmp
            [1] => E:\xampp\tmp\php84AC.tmp
            [2] => E:\xampp\tmp\php84BC.tmp
            [3] => E:\xampp\tmp\php84CD.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [size] => Array
        (
            [0] => 50891
            [1] => 40805
            [2] => 16985
            [3] => 1800
        )

)