简体   繁体   中英

Checking if input type file multiple is empty in php

I want to check if an input type file with an attribute of multiple is empty, I've tried to check if count is equal to 0 ie if(count($_FILES['file'])){} but it's not working because even an empty array has a count of 5(the number I got when I echoed out the count), I have also tried using the empty function ie if(empty($_FILES['file'])){} not also working, please help me out. Thanks

You should count a info directive of the files instead of counting the file entry for the input name. Once you have multiple files being uploaded the arrays of the info directives extends to having multiple entries at all info directives at the input named entry.

if ( isset($_FILES['file'] && count($_FILES['file']['name']) > 0 ) {
  // ...
}

Or with PHP 7.x:

if ( count($_FILES['file']['name'] ?? []) > 0 ) {
  // ...
}

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