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.

CODE:

$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);


ERROR:

{"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: -
Picture of Patrick Liersch
Re: Problem with function core_files_upload
 

And everyone has tested this example and works?

I tried this and got a error "nofile" - please tell me, where is my fail?


https://<HOST>/<MOODLE>/webservice/rest/server.php?moodlewsrestformat=json&wstoken=<TOKEN FROM OUR WS USER>&wsfunction=core_files_upload&contextlevel=user&instanceid=<USER ID>&component=user&filearea=draft&itemid=0&filepath=/&filename=test&filecontent=hello
 
Average of ratings: -
Picture of Aniket Sankpal
Re: Problem with function core_files_upload
 

Don't pass the parameter itemid and then try

 
Average of ratings: -
Picture of Patrick Liersch
Re: Problem with function core_files_upload
 

The Parameter itemid is required and must be a int value

 
Average of ratings: -
Picture of Aniket Sankpal
Re: Problem with function core_files_upload
 

Can you tell me how you are calling the service?

 Are you using POST method?

 
Average of ratings: -