Update sotred file using File API

Update sotred file using File API

by Abdulhadi Zakkour -
Number of replies: 2

Is it possible to change the path of a stored file through file API?

I'm aware I can get file contents as a string then make a new file based on that string and delete the old file but I want to see if there is a shorter path useing file API. I also understand that I can just use $DB->set_field but that might be my last resort because direct databse modifications are discouraged from what I understood in the documentation.

Average of ratings: -
In reply to Abdulhadi Zakkour

Re: Update sotred file using File API

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You could try:

$newfile = $fs->create_file_from_storedfile($newdetails, $oldfile);
$oldfile->delete();

This won't result in lots of data being transferred, just a single DB insert, followed by a DB delete.

And you're right about set_field - you really don't ever want to bypass the Moodle Files API like that, as you're asking for trouble ...
Average of ratings: Useful (2)
In reply to Davo Smith

Re: Update sotred file using File API

by Abdulhadi Zakkour -
I was searching for this function in the stored_file class reference but it never occured to me that I should search in file_storage class. Learning Moodle development is fun despite the challenges.

Thanks for helping, Davo.