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?