Monday 1 December 2014

Moodle: list files in moodle filesystem

Hello,

Last day we saw how to create an excel file in modle: http://icsbcn.blogspot.com/2014/11/moodle-create-and-save-excel-file-in.html

If we want to list that files, we can do this:

// List excel files
$table = new html_table();
$table->head = array('filename', 'filedate', 'filesize', '');
$table->align = array('left', 'left', 'left','left');


// Get files
$fs = get_file_storage();
$files = $fs->get_area_files(0,  'backup', 'course', 0, 'timemodified DESC', false);
foreach ($files as $file) {
$filename = $file->get_filename();


$row = array();
$row[] = $filename; // filename
$row[] = date('d/m/Y H:i:s', $file->get_timecreated()); // creation date
$row[] = $this->formatSizeUnits($file->get_filesize()); // file size as human view


// Download link
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename);
$row[] = html_writer::link($url, get_string('downloadfile', 'moodle'));


$table->data[] = $row;
}
// End get files


$content .= html_writer::start_tag('div', array('id' => 'list_files'));
$content .= html_writer::table($table);
$content .= html_writer::end_tag('div');
// End list excel files

Best regards!

No comments:

Post a Comment