Tuesday 2 December 2014

Moodle: Delete one file in moodle filesystem

Hola,

After seeing how to add files and how to list them. Today we will see how to delete them:

$fs = get_file_storage();

// Prepare file record object
$fileinfo = array(
'component' => 'backup',
'filearea' => 'course',
'itemid' => 0,
'contextid' => 0,
'filepath' => '/',
'filename' => 'filename.xlsx'
); 


// Get file
$file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'],
$fileinfo['filename']);

// Delete it if it exists
if ($file) {
if ($file->delete()) {
return true;
} else {
return false;
}
}


Best regards!

No comments:

Post a Comment