Multiupload function not send many file to serwer

Hi,
I have this code:


    controller:
        public function uploadFiles()
            {
                if (isset($_FILES['file']) && count($_FILES['file']['name']) > 0) {
                    $acceptFormat = array(
                        'jpeg' => 'image/jpeg',
                        'jpg' => 'image/jpg',
                        'png' => 'image/png'
                    );
        
                    $folder = "system";
                    $destinationFilePath = "../" . $this->_config->upload_catalog_name . "" . $folder;    
        
                    $nazwy = array();
                    for ($i = 0; $i <= count($_FILES['file']['name']) + 1; $i++) {
                        $newFileName = $this->uploadFile1($_FILES['file'][$i], $destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $acceptFormat);
                    }
        
                }
            }
        
     function:   
        private function uploadFile1(array $filesArray, string $destinationFilePath, bool $fileValidation = true, string $fileType, int $category = 0, array $acceptFormat):string
            {
                for ($i = 0; $i <= count($filesArray['name']) + 1; $i++) {
        
        
                        try {
                            if (
                                !isset($filesArray['error'][$i]) ||
                                is_array($filesArray['error'][$i])
                            ) {
                                   throw new RuntimeException('Invalid parameters.');
                            }
        
                            switch ($filesArray['error'][$i]) {
                                case UPLOAD_ERR_OK:
                                    break;
                                case UPLOAD_ERR_NO_FILE:
                                    throw new RuntimeException('No file sent.');
                                case UPLOAD_ERR_INI_SIZE:
                                case UPLOAD_ERR_FORM_SIZE:
                                    throw new RuntimeException('Exceeded filesize limit.');
                                default:
                                    throw new RuntimeException('Unknown errors.');
                            }
        
                            if ($filesArray['size'][$i] > 1000000000) {
                                 throw new RuntimeException('Exceeded filesize limit.');
                            }
        
                            $ext = strtolower(pathinfo($filesArray['name'][$i], PATHINFO_EXTENSION));
                            if (!in_array($ext, array_keys($acceptFormat)) && $fileValidation == true) {
                                throw new RuntimeException('Invalid file format.');
                            }
        
                            $mime = mime_content_type($filesArray['tmp_name'][$i]);
                            if (!in_array($mime, array_values($acceptFormat)) && $fileValidation == true) {
                                throw new RuntimeException('Invalid mime format.');
                            }
        
                            require_once $this->_config->function_path . "RandomFileName.php";
                            $pathTmpName = pathinfo($filesArray['name'][$i]);
                            $fileExtension = strtolower($pathTmpName['extension']);
        
                            $randomName = randomFileName(60);
        
                            $newFileName = $randomName . "." . $fileExtension;
        
                            if (!file_exists($destinationFilePath)) {
                                mkdir($destinationFilePath, 0777);
                            }
                            if (!file_exists($destinationFilePath . "/thumbs")) {
                                mkdir($destinationFilePath . "/thumbs", 0777);
                            }
        
                            if (!move_uploaded_file($filesArray['tmp_name'][$i], $destinationFilePath . "/" . $newFileName)) {
                                throw new RuntimeException('Failed to move uploaded file.');
                            } else {
                                return $newFileName;
                            }
        
                        } catch (RuntimeException $e) {
                            echo $e->getMessage();
                            return "";
                        }
                }
            }

I need a universal function to send files to a server in php. It can be 1 file - and it can be 10 at a time.

At the moment I send 1 file to me, despite sending eg 5 files.

As a result of this function I want to get a new file name on the server.
To do this I made such a call:

    for ($ i = 0; $ i <= count ($ _ FILES ['file'] ['name']) + 1; $ i ++) {
                             $ newFileName = $ this-> uploadFile1 ($ _ FILES ['file'] [$ i], $ destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $ acceptFormat);
                         }

Does anyone know how to fix it?

What is in $_FILES when you try to use the code to upload two files or more? Is it containing what you think it should contain?

I have read elsewhere that if your HTML code is just adding the “multiple” parameter to the file input, you also need some JS code to gather the list of filenames to submit, otherwise you’ll just get the first one. I’m not 100% sure on that, I just have a vague recollection of it.

In $_FILES I have this:


Array
(
    [name] => Array
        (
            [0] => X+T+rcffQvCG67MuqR49Vg.jpg
            [1] => X1pHOaBFRtGyXrA0FoDBVg.jpg
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => image/jpeg
        )

    [tmp_name] => Array
        (
            [0] => /Applications/XAMPP/xamppfiles/temp/php2fuLup
            [1] => /Applications/XAMPP/xamppfiles/temp/phpZIoKbH
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 1965954
            [1] => 1557849
        )

)

(for 2 files)

Is there an array element called $_FILES['file']['name'] ? Isn’t it just $_FILES['name'] ?

Assuming that’s not the issue, isn’t it a case of capturing the return from your uploadFile1() function and performing the move operation into that filename inside your loop? I may not be properly understanding the question.

The problem is receiving files via this function. It always downloads one file - and it should be many :frowning: The element you are writing about is available

Can you show the code for the randomfilename() function?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.