I would like to create a plugin/function to move all unused assets (which are not related to a entry) to a specific folder in the same assets source. Now i try to create the function to move a single file by referring the id of the file. But i get always errors... The function should look like this:

public function moveFileToFolder($fileId,$idOfTargetFolder,$assetSourceId) {

Thanks for any help!

1 Answer 1


I found the solution: The moveFiles function provide the function to move a file:

* Move or rename files.
* @param        $fileIds
* @param        $folderId
* @param string $filename If this is a rename operation or not.
* @param array  $actions  Actions to take in case of a conflict.
* @throws Exception
* @return bool|AssetOperationResponseModel

public function moveFiles($fileIds, $folderId, $filename = '', $actions = array())

So for me the following code works to move the file with the id 21 to the folder with the id 3:

craft()->assets->moveFiles([21], 3,'', "keepBoth");
  • 1
    Thanks for sharing! I kept getting an Undefined offset: 1 error when moving multiple files though. So if you try this, remember to make $actions an array with the same length as $fileIds. Like this: craft()->assets->moveFiles([21, 22], 3,'', ['keepBoth', 'keepBoth']); Jan 17, 2017 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.