I am using this PHP Code:
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!empty($_FILES['ticket_files']))
{
}
}
But if the file input is blank, it still thinks that there is a file there and runs the code.
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if (count($_FILES['ticket_files']) > 0)
{
}
}
Try this instead of what you have now.
If nothing is uploaded $_FILES array looks like:
Array ( [image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
The error code 4 [error] => 4 indicates no file was uploaded and error code 0 indicates no error and file was uploaded. You can add below check instead of empty check.
if($_FILES['ticket_files']['error']==0) {
// file uploaded, process code here
}
empty()does not check contents of files, it checks content of variables.$_FILES['ticket_files']points to an array of information about the file uploaded - includingname- so it will never be empty if a file was selected.