3

I am trying to get the url to a file that I have uploaded to moodle. I want to access the file via the browser using http. The pathname of the file is hashed in the moodle database. Is there a way of getting the real url of uploaded files in moodle? this is the code I was trying out using the Moodle File API.

<?php
require_once("../config.php");
$course_name=$_GET["course"];
$table_files="files";
$results=$DB->get_records($table_files,array('filename'=>$course_name));
//Get the file details here::::
foreach($results as $obj){
        $contextid=$obj->contextid;
        $component=$obj->component;
        $filearea=$obj->filearea;
        $itemid=$obj->itemid;
}
$url=$CFG->wwwroot/pluginfile.php/$contextid/$component/$filearea/$itemid/$course_name;
echo print_r($url);

?>

Will appreciate the help.

John Kulova
  • 477
  • 1
  • 10
  • 20
  • What version of Moodle? – Drew Jul 25 '13 at 12:41
  • I'm not entirely sure what you mean by "real URL". Are you just trying to generate the URL needed to request the file from Moodle or are you wanting to bypass that somehow and go more directly to the file? – Mark Jul 26 '13 at 10:50

1 Answers1

2

This works for Moodle version 2.3

<?php
    require_once("../config.php");

    $filename = "my_image.jpg";
    $table_files = "files";
    $results = $DB->get_record($table_files, array('filename' => $filename, 'sortorder' => 1));
    $baseurl = "$CFG->wwwroot/pluginfile.php/$results->contextid/$results->component/$results->filearea/$results->itemid/$filename";
    echo $baseurl;

The result is the Url to the image "my_image.jpg"

nejib
  • 76
  • 3