## General developer forum

### How to find a URL for a file

How to find a URL for a file

Hi Please anyone can help me to find the URL location of file within a Course.

i have below requirement to build a AdHoc reports .. i managed to to find all other filed except the Section(URL location of file within a Course)

A CSV containing details of all certain file types (Word, Powerpoint or PDF) or all files with course title, file title and size, and location of that file within a course. See below image

Average of ratings: -
Re: How to find a URL for a file
This is a tricky question depending on exactly what you want.  The simplest answer is just to look at files that are attached to File resources.  The File resource creates a course module instance, which has the section number in the record.

If you can share the code you've used so far to fetch your list of files, I or someone else might be able to show you how to get the section number from there.

Average of ratings: -
Re: How to find a URL for a file

Hi Mark,    Thanks for suggestion here is the code i  am using in the query.

SELECT co.fullname as Course,

mf.filename as [Media Title],

mf.filesize/(1024*1024) AS [Filesize(MB)],

mf.mimetype as [Media Type],

'' as [Section]

FROM mdl_files mf

join mdl_context cx on cx.id = mf.contextid

join mdl_course_modules cm on cm.id = cx.instanceid

join mdl_modules mo on mo.id = cm.module

join mdl_course co on co.id = cm.course

Average of ratings: -
Re: How to find a URL for a file

Ok, you're already joining on the course_modules table, you can just do "cm.section as section" in your SELECT clause.

Average of ratings: -
Re: How to find a URL for a file

that is just returning a Section number So how do i get a URL for that file location...

Average of ratings: -
Re: How to find a URL for a file

You can just build it up from the data you've got:

CONCAT('/course/view.php?id=', co.id, '#section-', cm.section) in SQL or

new moodle_url('/course/view.php', ['id' => $record->id], 'section-' .$record->section); in PHP

Average of ratings: -