简体   繁体   中英

Uploading file to Moodle using the REST Service core_files_upload

I am developing an Android app, which will upload content to a user's private files in my Moodle installation using the REST webservice core_files_upload provided by Moodle. core_files_upload takes the following parameters:

contextid
component
filearea
itemid
filepath
filename
filecontent

The documentation for the Moodle Web Services isn't very detailed so I have pieced together what I can from the Moodle forums and Google searches, but I feel I have come to a dead end. From examples I have seen that the parameters take the following values:

contextid: int - not sure whether this is the userid
component: "user"
filearea: "private"
itemid: 0
filepath: "/"
filename: "filename.jpg"
filecontent: base64 encoding of file

I am using my userid as the contextid - I'm usnsure whether this is correct due to lack of documentation. When I post this I receieve the error:

{"exception":"moodle_exception","errorcode":"nofile","message":"File not specified"}

I have looked at where core_files_upload is defined in "moodle/files/externallib.php" and this error message is generated when filecontent is not present.

I have tried posting to a test script and I can successfully create the image on the Moodle server based on the base64 encoding eg:

<?php
file_put_contents('MyFile.jpg', base64_decode($_POST['filecontent']));

Can anyone shed light on why I am being unsuccessful uploading to Moodle?
Is the contextid the userid of the user doing the upload?

Ok, so after a bit of further reading and hacking around with code I can confirm that the context id is not the user id, but is derived from the user id. I have not found any way in Moodle to get the the context id, so I have created my own. Once I had the context id for the user, the upload worked.

define('AJAX_SCRIPT', true);
define('NO_MOODLE_COOKIES', true);

require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once($CFG->dirroot . '/webservice/lib.php');

echo $OUTPUT->header();

// authenticate the user
$token = required_param('token', PARAM_ALPHANUM);
$webservicelib = new webservice();
$authenticationinfo = $webservicelib->authenticate_user($token);

// check the user can manage his own files (can upload)
$context = context_user::instance($USER->id);
$result = array("contextid"=>$context->id);
echo json_encode($result);

Hope this helps anyone else who faces the same problem.

  • contextlevel : The context level to put the file in, (block, course, coursecat, system, user, module)

  • instanceid : The Instance id of item associated with the context level

If you specify contextlevel=user then instanceid must be the user id in DB.

maybe can someone post a correct solution.

I will use the Webservice API (core_files_upload) from Moodle and the UserID as Parameter.

$fileinfo = array(
'contextid' => null,
'component' => 'user',
'filearea' => 'draft',
'itemid' => 0,
'filepath' => '/',
'filename' => 'sample.txt',
'filecontent' => base64_encode("Hello Word!"),
'contextlevel' => 'user',
'instanceid' => $userid,
);

The error is ever "file not specified" - Moodle upload this file to a temp Directory, but then stopped and answer with the error message

After spending three days researching on the moodle core_files_upload function I managed to come up with this optimal solution. Hope this will help someone in future.

<?php
 $token = 'Put your token here';
 $domainname = 'Put your app url here';
 $restformat = 'json';
require_once('curl.php'); //Download this file
$curl = new curl;
$params = array(
    'component' => 'user',
    'filearea' => 'draft',
    'itemid' => 0,
    'filepath' => '/',
    'filename' => 'moodle.PNG',
    'filecontent' => base64_encode('/images/moodle.PNG'),
    'contextlevel' => 'user',
    'instanceid' => $userid,
);

/// UPLOAD IMAGE - Moodle 3.10+ and later
$functionname = 'core_files_upload';
$serverurl = $domainname .'/webservice/rest/server.php' . '?wstoken=' . $token .'&wsfunction=' . $functionname;

$restformat = ($restformat == 'json') ?
'&moodlewsrestformat=' . $restformat : '';
print_r($params);
printf("\n");
printf("\n");

$resp = $curl->post($serverurl . $restformat, $params);
$resps = json_decode($resp);

print_r($resps);
printf("\n");

Also check this https://moodle.org/mod/forum/discuss.php?d=275796 I have tested and it's working as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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