Filemanager - saving to specified directory

Filemanager - saving to specified directory

by Conn Warwicker -
Number of replies: 4
Picture of Core developers Picture of Plugin developers

Hi,

We are trying to use the filemanager to allow us to upload several files to a particular directory within our moodledata folder.

We've got it working in the standard way so far, so that it saves the records to the mdl_file table and presumably saves them somewhere in the moodledata/filedir directory, though I have no idea where, since it's all hashed file names and folders.

But we don't want to save them to those locations, we literally just want to upload the files we choose, to a particular folder within moodledata, with the same filenames as uploaded.

We can do this with a filepicker element:

class myform extends moodleform

{

    

    public function definition()

    {

                

        $mform = $this->_form;

        $mform->addElement('filepicker', 'myfile', 'My File');

        $mform->addElement('submit', 'mysubmit', 'Upload');        

        

    }

    

}


$form = new myform();

if ($data = $form->get_data())

{

    $filename = $form->get_new_filename('myfile');

    $form->save_file('myfile', $CFG->dataroot . '/datauploads/current/' . $filename);

}


But can't seem to work out how to do it with filemanager. I'm trying:


    $form->save_file('myfile', $CFG->dataroot . '/datauploads/current/');


Just passing the directory we want all the files to be saved to, but looking in the save_file method in formslib.php it's returning false because of the file_exists() call, because the directory exists, so it fails. So I have no idea how to tell it where to save the files.

The save_files() method seems to be deprecated, so we can't use that any more.


Is there a way to do this with the filemanager, or should we just write a custom php file upload instead?


Thanks.

Average of ratings: -
In reply to Conn Warwicker

Re: Filemanager - saving to specified directory

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, if you really need the files saved in a directory, rather than inside the files API (you haven't explained why this is a requirement, but I'll assume for the moment that you have a good reason for doing so), then the following code should work.

$fs = get_file_storage();
$files = $fs->get_area_files($contextid, $component, $filearea, $itemid, 'filename', false);
foreach ($files as $file) {
$file->copy_content_to($destdir.'/'.$file->get_filename()); }

This code assumes you've already used the standard Moodle functions to save the files from the draft area into a known location (although, in theory, you could use the $contextid, $component, $filearea and $itemid of the draft area to do this). If you want to, you could add a $file->delete() call inside the loop to delete the files from the Moodle API at the same time.

I've also assumed you don't want the subdirectories (only the files themselves), so I've passed 'false' as the last param to get_area_files.


Average of ratings: Useful (3)
In reply to Davo Smith

Re: Filemanager - saving to specified directory

by Lloyd D -

I've been trying to do the same thing... I want to save from the file manager to my own specified directory but I don't want it to be initially saved in the default Moodledata folder.. also I tried changing to use a filepicker in my form but when I try and save the file in the save_settings function it does not have access to the MoodleQuickForm class as it's not parsed/available in the function. I'd really appreciate any help on this problem

In reply to Lloyd D

Re: Filemanager - saving to specified directory

by Lloyd D -

Got it working! Thanks so much for that code Davo

In reply to Davo Smith

Re: Filemanager - saving to specified directory

by Lloyd D -

Hi Davo, how can I load the files that are copied to the specified destination into the file manager? At the moment the files are:

  1. saved to the default Moodledata folder
  2. then copied to the specified directory
  3. then the files are loaded from the Moodledata folder into the file manager.

How can I load the files from the directory in which the files were copied to?