Assuming that you have added the element like this :
$mform->addElement('filepicker', 'file', "Upload a Document", null, array('maxbytes' => 1024*1024, 'accepted_types' =>array('*.png', '*.jpg', '*.gif','*.jpeg', '*.doc', '*.rtf','*.pdf','*.txt')));
Now assuming that You get the data as the following
$data = $lesson_form->get_data()
See the code below to upload the file to a specified folder in your server. This is compatible with moodle 2.2+
$realfilename = $lesson_form->get_new_filename('file'); // this gets the name of the file
$random =rand(); // generate some random number
$new_file = $random.'_'.$realfilename; //add some random string to the file
$dst = "uploads/$new_file"; // directory name+ new filename
if($realfilename !=''){ // checking this to see if any file has been uploaded
save_files($dst); // moodle function to save a file in given folder
}
I faced the same problem that you're facing and it solved my problem.
N.B. -> Remember to chmod your upload folder to 0777.