I'm trying to set up a Jodit WYSIWYG and I'm finding their documentation rather confusing. I'm looking at a way to change the file name while uploading.
cause: if I upload the file name with the same name in the root directory it will be replaced the old file.
So, How changing file name while uploading for voiding replace the old name file.
here procedure is upload Image 3 files 1.jpg,2.jpg,3.jpg
when Debug mode the name of the file is in
$files = $_FILES[$source->defaultFilesKey];
in function
public function move(Config $source) {
$files = $_FILES[$source->defaultFilesKey];
/**
* @var $output File[]
*/
$output = [];
try {
if (isset($files) and is_array($files) and isset($files['name']) and is_array($files['name']) and count($files['name'])) {
foreach ($files['name'] as $i => $file) {
if ($files['error'][$i]) {
throw new \Exception(isset(Helper::$upload_errors[$files['error'][$i]]) ? Helper::$upload_errors[$files['error'][$i]] : 'Error', $files['error'][$i]);
}
$path = $source->getPath();
$tmp_name = $files['tmp_name'][$i];
$new_path = $path . Helper::makeSafe($files['name'][$i]);
if (!move_uploaded_file($tmp_name, $new_path)) {
if (!is_writable($path)) {
throw new \Exception('Destination directory is not writeble', Consts::ERROR_CODE_IS_NOT_WRITEBLE);
}
throw new \Exception('No files have been uploaded', Consts::ERROR_CODE_NO_FILES_UPLOADED);
}
$file = new File($new_path);
try {
$this->accessControl->checkPermission($this->getUserRole(), $this->action, $source->getRoot(), pathinfo($file->getPath(), PATHINFO_EXTENSION));
} catch (\Exception $e) {
$file->remove();
throw $e;
}
if (!$file->isGoodFile($source)) {
$file->remove();
throw new \Exception('File type is not in white list', Consts::ERROR_CODE_FORBIDDEN);
}
if ($source->maxFileSize and $file->getSize() > Helper::convertToBytes($source->maxFileSize)) {
$file->remove();
throw new \Exception('File size exceeds the allowable', Consts::ERROR_CODE_FORBIDDEN);
}
$output[] = $file;
}
}
} catch (\Exception $e) {
foreach ($output as $file) {
$file->remove();
}
throw $e;
}
return $output;
}
so $file is keep the name file in array
i need to foreach $fiels and change array['name'] value. but i don't konw how to change it.
UPDATE after I try i got a solution use foreach statment for loop $files['name'].
public function move(Config $source) {
$files = $_FILES[$source->defaultFilesKey];
foreach ($files['name'] as $i => $file) {
$files['name'][$i] = round(microtime(true)).($files['name'][$i]);
}
/**
* @var $output File[]
*/
$output = [];
.
.
.
}