How to handle multiple file upload using PHP

后端 未结 5 713
-上瘾入骨i
-上瘾入骨i 2020-11-29 07:57

I want to upload files using PHP but the problem is that I don\'t know how many files I will upload.

My question is how can I upload files if I use file[]

5条回答
  •  一生所求
    2020-11-29 08:37

     if (isset($_FILES['file']['tmp_name'])) {
                for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) {
                    $tmpFilePath = $_FILES['file']['tmp_name'][$i];
                    if ($tmpFilePath != "") {
    //. time() . $_FILES['file']['name'][$i] becomes the name of the files
                        $file[$i] = $newFilePath = "upload/myfolder/" . time() . $_FILES['file']['name'][$i];
                        if (move_uploaded_file($tmpFilePath, $newFilePath)) {
                            $file[$i] = $newFilePath = "upload/myfolder/" . time() . $_FILES['file']['name'][$i];
                        }
                    }
                }
            }
    

    PUT THIS SIMPLE SCRIPT INTO PHP SCRIPT OR FUNCTION. NB:

    $_FILES['file']

    because

提交回复
热议问题