Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Метод для сохранения файла #41

Open
Alex300 opened this issue Nov 9, 2024 · 0 comments
Open

Метод для сохранения файла #41

Alex300 opened this issue Nov 9, 2024 · 0 comments

Comments

@Alex300
Copy link
Owner

Alex300 commented Nov 9, 2024

Надо вынести процедуру сохранения файла существующего в локальной файловой системе сервера в отдельный метод сервиса.
см. https://www.cotonti.com/forums?m=posts&q=9374

т.е. это должно быть в отдельном методе:

// Путь к файлу локально
$localFileName;

$file = new File();
$file->original_name = $originalName;
$file->user_id = $uid;
$file->source = 'shop';
$file->source_id = $product->id; // ID сохраненного товара
$file->ext = mb_strtolower(cot_filesGetExtension($fileData->fileName));
$file->mime_type = mime_content_type($localFileName);
$file->is_img = 1;
$file->size = filesize($localFileName));


Cot::$db->beginTransaction();
try {
	$fileId = $file->save();
	if (!fileId) {
		throw new Exception('Ошибка при сохранении');
	}
	
	// Path relative to files root directory
	$relativeFileName = FileService::generateFileRelativePath($objFile);
	$file->path = dirname($relativeFileName);
	$file->file_name = basename($relativeFileName);
	$file->filesystem_name = FileService::getFilesystemName($file->source, $file->source_field);
	 
	// Path relative to site root directory
	$fileFullName = Cot::$cfg['files']['folder'] . '/' . $relativeFileName;
	if (!@rename($localFileName, $fileFullName)) {
		throw new Exception('Ошибка при сохранении');
	} 
	 
	$file->save();
	 
    Cot::$db->commit();
} catch(\Throwable $e) {
    Cot::$db->rollBack();
    // не удалось сохранить файл;
}
``

Который могут использовать разные контроллеры модуля или другие расширения.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant