General developer forum

Upload and display file in moodledata folder

 
Picture of Inamullah Khan
Re: Upload and display file in moodledata folder
 

I defined this fucntion in lib.php and now file is downloading but it show error when i open downloaded pdf file. pdf

Here is  block_course_status_tracker_pluginfile function

function block_course_status_tracker_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {

    global $DB;

    if ($context->contextlevel != CONTEXT_BLOCK) {

        return false;

    }

    require_login();

    if ($filearea != 'attachment') {

        return false;

    }

    $itemid = (int)array_shift($args);

    if ($itemid != 0) {

        return false;

    }

    $fs = get_file_storage();

    $filename = array_pop($args);

    if (empty($args)) {

        $filepath = '/';

    } else {

        $filepath = '/'.implode('/', $args).'/';

    }

    $file = $fs->get_file($context->id, 'block_course_status_tracker', $filearea, $itemid, $filepath, $filename);

    if (!$file) {

        return false;

    }

    // finally send the file

    send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!


 
Average of ratings: -