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:
public 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 "";
}
}
}
$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:
for ($i = 0; $i <= count($_FILES['file']['name']) + 1; $i++) {
$newFileName = $this->uploadFile1($_FILES['file'][$i], $destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $acceptFormat);
}
Problem w tym że nie działa to poprawnie :(
Dane odebrane wyglądają w sposób następujący:
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
)
)
Wie ktoś może jak poprawić mój kod żeby to zaczęło działać?
Bardzo proszę o pomoc.