Copy file from one location to another in moodledata

Copy file from one location to another in moodledata

by Raymond Mlambo -
Number of replies: 4

Hi,

How can I programmitically do the following file processes in moodle:

  • Create a folder in moodledata (e.g., $CFG->dataroot.'/custom_folder')
  • Move a file from a moodle activity (e.g., mod_forum) to this particular folder

All I need is how I can create the folder, and move a file into that folder. 

I already have code for fetching and listing files uploaded by students in the forum activity module.

Average of ratings: -
In reply to Raymond Mlambo

Re: Copy file from one location to another in moodledata

by Raymond Mlambo -

Can someone help here please, I really need to do this. I've already done all the donkey work of locating the files and displaying them to a user (that File API aint easy!), from any activity module.

Please please please!!

In reply to Raymond Mlambo

Re: Copy file from one location to another in moodledata

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Assuming you have a stored_file instance $file, then you can just call the following:

$file->copy_content_to($pathname);

Reference.

Average of ratings: Useful (1)
In reply to Paul Holden

Re: Copy file from one location to another in moodledata

by Raymond Mlambo -

Hi Paul,

Its not working very well. When I turn debug on, it shows the following:

Warning:  copy(): The second argument to copy() function cannot be a directory in /var/www/dev/lib/filestorage/stored_file.php on line 479

Here is a bit of my code where I'm trying to copy the file:

   $fs = get_file_storage();
   $files = $fs->get_area_files($modulecontext->id, 'mod_peerassessment', 'attachment', $peerassessment_file->itemid, 'sortorder DESC, id ASC', false); 
       foreach ($files as $file) {
            if (!file_exists($CFG->dataroot . '/peerassessments/' . $peerassessment->id . '/' . $student->id)) {
             mkdir($CFG->dataroot . '/peerassessments/' . $peerassessment->id . '/' . $student->id);
              }
              if (($file->copy_content_to($CFG->dataroot . '/peerassessments/' . $peerassessment->id . '/' .$student->id))
                  ) {
         $path = '/' . $modulecontext->id . '/mod_peerassessment/attachment/' . $peerassessment_file->itemid . '/' . $filename;
          $fullurl = moodle_url::make_file_url('/pluginfile.php', $path);
         echo html_writer::link($fullurl, $filename, array('class' => 'studentfile'));

                                  

In reply to Raymond Mlambo

Re: Copy file from one location to another in moodledata

by Raymond Mlambo -

Hi Paul, sorry, I got it this time.

I changed the code a bit to include the filename, after specifying the new directory where I want the file to be copied to. After adding the filename to it, it worked beautifully!! New line of code now:

($file->copy_content_to($CFG->dataroot . '/peerassessments/' . $peerassessment->id . '/' . $student->id.'/'.$file->get_filename())

Thank you for the help!