繁体   English   中英

如何在moodle中获取上传的文件路径

[英]how to get an uploaded file path in moodle

我创建了一个新活动,并在其中正确上传了一个文件,但是当我使用

$url = moodle_url::make_pluginfile_url($file->contextid, $file->component, $file->filearea, $file->itemid,$file->filepath, $file->filename);

获取链接结果是:对不起,找不到请求的文件

我搜索了一个我发现我必须在我的库中添加一个插件文件 function,这是我的 function:

function testnew_pluginfile($course, $cm, $context, $component, $filearea, $itemid, $path, $filename) {

    global $CFG, $DB;
    require_course_login($course, true, $cm);
    $fullpath = "/{$context->id}/$component/$filearea/$itemid/$path/$filename";
    $fs = get_file_storage();
    if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
    return false;
    }
    send_stored_file($file, 0, 0, false);
    }

问题是上下文 ID 没有实现。 所以我添加了我的 add_instance function 以在 mdl_files 表中添加一个新的上下文 ID。 这是我的 function:

function testnew_add_instance($data){
    global $DB;
    $cmid = $data->coursemodule;
    $data->timemodified =time();

    $data->id = $DB->insert_record('testnew', $data);
    $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
    $fs = get_file_storage();
    $draftitemid = $data->type;
    $context = context_module::instance($cmid);
    file_save_draft_area_files($draftitemid, $context->id, 'mod_testnew', 'content', 0);
    $files = $fs->get_area_files($context->id, 'mod_testnew', 'content', 0, 'sortorder', false);
    if (count($files) == 1) {
        // only one file attached, set it as main file automatically
        $file = reset($files);
        file_set_sortorder($context->id, 'mod_testnew', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
    }
    return $data->id;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM