Reputation: 4192
I need to store a file in Moodle. This is not really a problem, it is explained here. The problem is that this file has to be accessible for everyone. Hence, there has to be a URL, e.g. www.mymoodlesite.com/temp/myfile.txt
or the like, which one can enter in ones browser and access the file. I thought of copying the file into the moodledata/temp folder, but then I do not have a URL in order to access the file..
Thanks for your help in advance!
Upvotes: 1
Views: 9530
Reputation: 4192
Finally I could solve my problem :-)
I used a filemanager like this:
$mform->addElement('filemanager', 'my_filemanager', 'Upload a file', null, array('maxbytes' => $CFG->maxbytes, 'maxfiles' => 1, 'accepted_types' => array('*.zip')));
Then saved the uploaded file like this:
if ($draftitemid = file_get_submitted_draft_itemid('my_filemanager')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_assignment', 'my_filemanager', 0, array('subdirs' => false, 'maxfiles' => 1));
}
The URL in order to access the uploaded file can then be created like this:
file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/my_filemanager');
Upvotes: 4
Reputation: 3070
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.
Upvotes: 2
Reputation: 1476
You can access files uploaded through moodle's file browser without being authenticated if the following is true - Your moodle site has forcelogin set to no - Your file is uploaded the the files in frontpage sitefiles.
Uploaded files are saved (assuming Moodle1.9) in moodledata/1/{filepath}. Since you have to do it programatically you can store them there and reference them using the url /file.php/1/{filepath}. To say it another way. Files saved to $CFG->datadir.'/1/'.filepath are accessible with $CFG->wwwroot.'/file.php/1/'.filepath;
Alternatively if you don't want the files to show up in your front page site files through the moodle file browser you could edit file.php to forget checking permissions for files located in your special directory and instead just serve them up.
Hope this is more helpful with this edit.
Upvotes: 0