General developer forum

Core_files_upload Error

 
Picture of Bruno Lollato
Core_files_upload Error
 

Hello everyone, I'm trying to upload a file using the function "core_files_upload", but I'm getting "file not specified" error.


my settings are:

contextid = 2

component = "user"

filearea = "draft"

itemid = 5

filepath = "/"

filename = model.pdf

filecontent = model.pdf converted to base64

contextlevel = "user"


the complete exception => exception: moodle_exception, errorcode: nofile, message: File not specified

 
Average of ratings: -
Picture of Patrick Liersch
Re: Core_files_upload Error
 

no chance... and no one want help.
I search above 2 Months for a solution.

Need this function for a extern Profilpic Upload.

 
Average of ratings: -
Picture of Matteo Scaramuccia
Re: Core_files_upload Error
Core developersParticularly helpful MoodlersPlugin developers

Hi Patrick,
could you share a simple script about how you're actually trying to use that API call? For example how did you evaluate the IDs? Example: https://stackoverflow.com/questions/20284425/uploading-file-to-moodle-using-the-rest-service-core-files-upload , keeping in mind https://docs.moodle.org/dev/Web_services_files_handling.

HTH,
Matteo

 
Average of ratings: -
Picture of Patrick Liersch
Re: Core_files_upload Error
 

Hi Matteo,

i tried different solutions.


$function = "core_files_upload";
$userid = 2; // admin
$contextid = 5; // SELECT id FROM `{$dbname}`.mdl_context WHERE instanceid = {userid} AND contextlevel = 30

// Prepare file record object
$fileinfo = array(
    'contextid' => 0, // ID of context
    'component' => 'user', // usually = table name
    'filearea' => 'draft', // usually = table name
    'itemid' => 0, // usually = ID of row in table
    'filepath' => '/', // any path beginning and ending in /
    'filename' => 'sample.txt',
    'filecontent' => base64_encode("Hello World!"), // any filename
    'contextlevel' => 'user',
    'instanceid' => $userid,
);

This via Rest API and CURL send to the Server - same error: file not specified


And this one:

http://moodle-dev.bildung-rp.de/musterinst55498/webservice/rest/server.php?moodlewsrestformat=json&wstoken=<TOKEN>&wsfunction=core_files_upload&contextlevel=user&instanceid=5&component=user&filearea=draft&itemid=1&filepath=/&filename=test&filecontent=hello


There are many discussions about this Problem and no Fix for our Problem.
https://moodle.org/mod/forum/discuss.php?d=338190


 
Average of ratings: -
Picture of Bruno Lollato
Re: Core_files_upload Error
 

Finally the upload works.

Heres what i did:

component = "user",

filearea = "draft",

itemid = 0,

filepath = "/",

filecontent = base64 converted file,

contextlevel = "user",

instanceid = user id


just ignore the contextid, since its impossible to found in moodle, just use the contextlevel with instanceid

 
Average of ratings: -
Picture of Patrick Liersch
Re: Core_files_upload Error
 

And where is now my failure?

array(8) {
  ["component"]=>
  string(4) "user"
  ["filearea"]=>
  string(5) "draft"
  ["itemid"]=>
  int(0)
  ["filepath"]=>
  string(1) "/"
  ["filename"]=>
  string(10) "sample.txt"
  ["filecontent"]=>
  string(16) "SGFsbG8gV2VsdCE="
  ["contextlevel"]=>
  string(4) "user"
  ["instanceid"]=>
  int(2)
}

ERROR: nofile "

 
Average of ratings: -
Picture of Patrick Liersch
Re: Core_files_upload Error
 

i have created now a Moodle Tracker Ticket.
https://tracker.moodle.org/browse/MDL-61276

The Problem is, the Webservice User must the same as the specified User at the Context.
Or u create for every User a Token and used this Token for your API Call.

 
Average of ratings: -