即使没有提交文件,PHP多次上传也会运行一次循环

I am trying to upload multiples files using PHP and HTML but even I set the validation in my code isset, the foreach loop runs once with empty submission.

<?php

    if (isset($_FILES['fileToUpload']))
    {

        foreach($_FILES['fileToUpload']['tmp_name'] as $key => $tmp_name)
        {
            $file_name = $key . $_FILES['fileToUpload']['name'][$key];
            $file_size = $_FILES['fileToUpload']['size'][$key];
            $file_tmp = $_FILES['fileToUpload']['tmp_name'][$key];
            $file_type = $_FILES['fileToUpload']['type'][$key];
            move_uploaded_file($file_tmp, getcwd() . "/" . time() . $file_name);
        }

        echo "Success";
    }
    else
    {
        echo "<form enctype='multipart/form-data' action='' method='POST'>";
        echo "File:<input name='fileToUpload[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";
        echo "</form>";
    }
?>

You have to check the file name whether it is empty or not before run the foreach loop using PHP

<?php
    if (isset($_FILES['fileToUpload']))
    {
        if($_FILES['fileToUpload']['tmp_name'][0] == "") {
            die("No files to upload");
        }
        else {
            // Now there are some files you can run upload method here
            foreach($_FILES['fileToUpload']['tmp_name'] as $key => $tmp_name) {}
        }
    }
?>

You can check errors for UPLOAD_ERR_NO_FILE or just check for general errors (as below).

<?php 
if($_FILES['userfile']['error'] == 0) { 
  // do something
} else { 
  // handle
}