Cannot download or use file url!

Cannot download or use file url!

by Le Quoc -
Number of replies: 4

Hi!,

I'm created a new module by using NEWMODULE template.

I added a File Manager file to upload files. I used this code to create a file link:

foreach ($files['files'] as $file) {
$filename = $file->get_filename();
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
$file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);

$out[] = html_writer::link($url, $filename);
}
$br = html_writer::empty_tag('br');

But when i click on the link, i got error: Sorry, the requested file could not be found.

When i edit my module, i still see the files i uploaded.

Can anyone tell me the right way to use file link.

Many thanks!

Quoc Huy

Average of ratings: -
In reply to Le Quoc

Re: Cannot download or use file url!

by Shashikant Vaishnav -

Hello Le,

You may try with this. That works for me.

foreach ($files as $file) {
$filename = $file->get_filename();
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
$file->get_filearea(), $file->get_filepath(), $filename, false);

$out[] = html_writer::link($url, $filename);
}
$br = html_writer::empty_tag('br');

return implode($br, $out);

This works ! Itemid not needed in URL. I am not sure but moodle doc for File API may be outdated 

gud luck,

Average of ratings: Useful (1)
In reply to Shashikant Vaishnav

Re: Cannot download or use file url!

by Le Quoc -

Thanks a lot.

It works like a charm.

Quoc Huy.

In reply to Le Quoc

Re: Cannot download or use file url!

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Have you put the MYPLUGIN_pluginfile() function in mod/MYPLUGIN/lib.php?

See examples in Moodle core for what that should look like (e.g. mod/page/lib.php, function page_pluginfile(); mod/forum/lib.php, function forum_pluginfile()).

If you have and it doesn't work, then try putting in 'echo' statements in the MYPLUGIN_pluginfile() function to figure out where it is going wrong.

In reply to Davo Smith

Re: Cannot download or use file url!

by Le Quoc -

Thanks for your useful link.

It works now!

Quoc Huy