General developer forum

Problem with function core_files_upload

Picture of Connor Dougan
Problem with function core_files_upload

I am making a webservice that needs to upload a file to a user's draft area. Looking at examples I ended up with the following code. However, I am getting a vague error telling me I have an invalid parameter and I can't figure out why. I have tried to follow examples as closely as I could.


$functionname = 'core_files_upload';

$filepath = '/'; 

// $base64 is just the base64 encoded value of my file

$params = array('component' => "user", 'filearea' => 'draft', 'itemid' => 0, 'filepath' => $filepath, 'filename' => 'test.pdf', 'filecontent' => $base64,);

$serverurl = $domainname . '/moodle/webservice/rest/server.php' . '?wstoken=' . $token . '&wsfunction=' . $functionname . '&moodlewsrestformat=json';

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_URL, $serverurl);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

$response = curl_exec($ch);


{"exception":"invalid_parameter_exception","errorcode":"invalidparameter","message":"Invalid parameter value detected"}

Average of ratings: -
Picture of Connor Dougan
Re: Problem with function core_files_upload

For those curious, to expand the error message you have to log into the moodle server as an admin and go to 

where you can change debugger messages to developer to find out more.

In this particular context, I need to either include contextid or the two parameters contextlevel and instanceid.

Since I want to upload to a user's draft area I would set contextlevel = 'user' and instanceid = $userid (which can be obtained via other methods).

I hope this helps someone!

Average of ratings: -