Witam serdecznie.
Mam formularz w którym wysyłam wiele plików naraz (obrazków).

Mam podstronę która odbiera te pliki za pomocą funkcji:

  1. public function uploadFile1(array $filesArray, string $destinationFilePath, bool $fileValidation = true, string $fileType, int $category = 0, array $acceptFormat):string
  2. {
  3. for ($i = 0; $i <= count($filesArray['name']) + 1; $i++) {
  4.  
  5.  
  6. try {
  7. if (
  8. !isset($filesArray['error'][$i]) ||
  9. is_array($filesArray['error'][$i])
  10. ) {
  11. throw new RuntimeException('Invalid parameters.');
  12. }
  13.  
  14. switch ($filesArray['error'][$i]) {
  15. case UPLOAD_ERR_OK:
  16. break;
  17. case UPLOAD_ERR_NO_FILE:
  18. throw new RuntimeException('No file sent.');
  19. case UPLOAD_ERR_INI_SIZE:
  20. case UPLOAD_ERR_FORM_SIZE:
  21. throw new RuntimeException('Exceeded filesize limit.');
  22. default:
  23. throw new RuntimeException('Unknown errors.');
  24. }
  25.  
  26. if ($filesArray['size'][$i] > 1000000000) {
  27. throw new RuntimeException('Exceeded filesize limit.');
  28. }
  29.  
  30. $ext = strtolower(pathinfo($filesArray['name'][$i], PATHINFO_EXTENSION));
  31. if (!in_array($ext, array_keys($acceptFormat)) && $fileValidation == true) {
  32. throw new RuntimeException('Invalid file format.');
  33. }
  34.  
  35. $mime = mime_content_type($filesArray['tmp_name'][$i]);
  36. if (!in_array($mime, array_values($acceptFormat)) && $fileValidation == true) {
  37. throw new RuntimeException('Invalid mime format.');
  38. }
  39.  
  40. require_once $this->_config->function_path . "RandomFileName.php";
  41. $pathTmpName = pathinfo($filesArray['name'][$i]);
  42. $fileExtension = strtolower($pathTmpName['extension']);
  43.  
  44. $randomName = randomFileName(60);
  45.  
  46. $newFileName = $randomName . "." . $fileExtension;
  47.  
  48. if (!file_exists($destinationFilePath)) {
  49. mkdir($destinationFilePath, 0777);
  50. }
  51. if (!file_exists($destinationFilePath . "/thumbs")) {
  52. mkdir($destinationFilePath . "/thumbs", 0777);
  53. }
  54.  
  55. if (!move_uploaded_file($filesArray['tmp_name'][$i], $destinationFilePath . "/" . $newFileName)) {
  56. throw new RuntimeException('Failed to move uploaded file.');
  57. } else {
  58. return $newFileName;
  59. }
  60.  
  61. } catch (RuntimeException $e) {
  62. echo $e->getMessage();
  63. return "";
  64. }
  65. }
  66. }
  67.  
  68. $newFileName = $this->uploadFile1($_FILES['file'][$i], $destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $acceptFormat);


W wyniku tej funkcji chciałbym otrzymać nazwę nowo wgranego pliku.

Wykombinowałem sobie odbiór plików w ten sposób:
  1. for ($i = 0; $i <= count($_FILES['file']['name']) + 1; $i++) {
  2. $newFileName = $this->uploadFile1($_FILES['file'][$i], $destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $acceptFormat);
  3. }



Problem w tym że nie działa to poprawnie sad.gif
Dane odebrane wyglądają w sposób następujący:
  1. (
  2. [name] => Array
  3. (
  4. [0] => X+T+rcffQvCG67MuqR49Vg.jpg
  5. [1] => X1pHOaBFRtGyXrA0FoDBVg.jpg
  6. )
  7.  
  8. [type] => Array
  9. (
  10. [0] => image/jpeg
  11. [1] => image/jpeg
  12. )
  13.  
  14. [tmp_name] => Array
  15. (
  16. [0] => /Applications/XAMPP/xamppfiles/temp/php2fuLup
  17. [1] => /Applications/XAMPP/xamppfiles/temp/phpZIoKbH
  18. )
  19.  
  20. [error] => Array
  21. (
  22. [0] => 0
  23. [1] => 0
  24. )
  25.  
  26. [size] => Array
  27. (
  28. [0] => 1965954
  29. [1] => 1557849
  30. )
  31.  
  32. )
  33.  


Wie ktoś może jak poprawić mój kod żeby to zaczęło działać?

Bardzo proszę o pomoc.