TransWikia.com

Craft adds unwanted date to filename

Craft CMS Asked by Maarten Heideman on July 21, 2021

How to remove the date behind a asset filename or prevent add a date behind the filename? I’ll have a plugin that’s modifies an existing pdf with SETA SIGN. To do so I’m copy the current asset, modify it in the temp folder, unlink original, copy modified file and unlink the temp file. I tried to rename the file (with $rename) but still the date is added behind the filename. I’ll found something with avoidFilenameConflicts but as far as I’ll see this is for new assets not for moving or renaming assets.

$assetFilePath = $asset->getVolume()->getRootPath() . DIRECTORY_SEPARATOR . $asset->getPath();
....
        unlink($assetFilePath);
        copy($tempPath . DIRECTORY_SEPARATOR . $asset->filename, $assetFilePath);
        unlink($tempPath . DIRECTORY_SEPARATOR . $asset->filename);

...
$newFileName = 'bla.pdf';
$rename = Craft::$app->assets->moveAsset($asset,$asset->getFolder(),$newFileName);

bla.pdf becomes bla_2021-03-26-124307.pdf but I’ll want just ‘bla.pdf’.

One Answer

Try using replaceAssetFile(), something like this (untested):

$rename = Craft::$app->assets->replaceAssetFile($asset->id,$asset->getFolder(),$newFileName);

Answered by Tammy Shipps on July 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP