Moodle upload custom file in PHP

How to upload custom file Moodle function

 if(isset($_FILES['imageactivity']) && !empty($_FILES['imageactivity'])){ 
 $file_url = $_FILES['imageactivity']['tmp_name'];
 $file_content = file_get_contents($file_url);
 $file_name = $_FILES['imageactivity']['name'];
 
 
 $fs = get_file_storage();
 $draftitemid = file_get_unused_draft_itemid(); 
 $contextid_local = 70;
 
 // Prepare file record object
 $fileinfo = array(
 'contextid' => 165, // ID of context 165
 'component' => 'user', // usually = table name
 'filearea' => 'draft', // usually = table name
 'itemid' => $draftitemid, // usually = ID of row in table 0
 'filepath' => '/', // any path beginning and ending in /
 'userid' => $USER->id,
 'source' => $file_name, 
 'filename' => $file_name); // any filename
 
 // Create file containing content
 $fs->create_file_from_string($fileinfo, $file_content); 
 
 file_prepare_draft_area($draftitemid, $contextid_local, 'local_metadata', 'image', 0); 
 file_save_draft_area_files($draftitemid, $contextid_local, 'local_metadata', 'image', $draftitemid); 
 }

Leave a Reply

Your email address will not be published. Required fields are marked *