General developer forum

update a file programmatically with file api?

 
Picture of A Guy
update a file programmatically with file api?
 

Good evening,

I have a plugin that presents a filemanager for the user to upload a file. I store it. All works as desired. I can even retrieve it.


I do retrieve and write it to disk for various things to be done to it. But after all of this work I want to upload the modified version back to mdl_files overwriting the existing file--the one I just downloaded.

I've tried $fs->create_file_from_pathname but of course like the footnote in the documentation states (although a little confusingly) you have to delete the original file first before you can do this one. It will work. But it updates/changes the file id which causes me issues for various reasons.

So I tried replace_file_with() on the file I download. Doesn't work as the file has to be a "stored file".


So is there really no way to programmatically download a stored file, change the contents, and write it back, overwriting what was there already without changing the file id?


Thank you.

 
Average of ratings: -
Picture of Justin Hunt
Re: update a file programmatically with file api?
Particularly helpful MoodlersPlugin developers

From Poodll code ....this works ..not sure how well it fits with your use case though ..

$newfile = $fs->create_file_from_pathname($draftfilerecord, $newfilepath);
$permanentfile = $fs->get_file_by_id($permfilerecord->id);
$permanentfile->replace_file_with($newfile);

 
Average of ratings: -
Picture of A Guy
Re: update a file programmatically with file api?
 

Ah. Makes sense. Thanks

 
Average of ratings: -