简体   繁体   中英

Validate multi-select fieid in php

I have one form with multi-select file field.now i want to check if file is selecetd or not on click of submit button.Currently i am using following code,

PHP Code:-

<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";
    }
}

?>

But after click in submit button if there is no image selected then also it shows File is selected meassage.so please give some solution.

$_FILES['files'] is a multidimensional array

So you have to check ur condition as

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

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

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

eg:

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
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM