Programmatically adding file modules

Programmatically adding file modules

by Amy Tucker -
Number of replies: 9

We'd like to programmatically add files, I've found the mdl_files table in the database which looks to be what we'd need to populate.


Most of the fields in there look pretty easy to replicate however contenthash and pathnamehash have both been encrypted before they are added.  I'm guessing this isn't just a standard md5? does anyone know how I'd replicate this?

Average of ratings: -
In reply to Amy Tucker

Re: Programmatically adding file modules

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

If you want to programatically add files, then start by reading through https://docs.moodle.org/dev/File_API and taking a look at the functions in lib/filestorage/file_storage.php - particularly the create_file_from_* functions, which give you lots of different ways of creating files inside Moodle.

What you should never attempt to do is to add entries directly into the mdl_files table - this will almost certainly cause problems (unless you go through the code and carefully replicate exactly what the Files API does internally - at which point, you'd have been better using the Files API directly in the first place).

Out of curiosity, what are you planning on doing with these files once you've added them?

Just adding files to the Moodle storage (via the API) won't actually make them appear anywhere in the front end. If you want the files to be available to users, you will need some sort of block or course activity to display them. In the case of adding a file activity, you will need to create a record in mdl_resource, create a cm object (stored in mdl_course_modules) to link this to a course, add it into a specific section of a course and then rebuild the course cache to allow it to appear. The code in course/dnduploadlib.php, starting with 'handle_file_upload' shows one example of how to do this.

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

Re: Programmatically adding file modules

by ss ss -
Hello, I'm desperate to do that, can you give me a head start about how to manipulate the file api or how its done I've read it several times and yet didn't get to apply it, how does my php shall start? please help!
In reply to ss ss

Re: Programmatically adding file modules

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

Well, there are lots and lots of examples throughout the Moodle code, but if you want a simple, standalone example (which is probably compatible with recent Moodle versions, although it hasn't been updated in a while), then try: https://github.com/AndyNormore/filemanager


In reply to Amy Tucker

Re: Programmatically adding file modules

by ss ss -

Hello, I'm desperate to do that, can you give me a head start about how to manipulate the file api or how its done I've read it several times and yet didn't get to apply it, how does my php shall start? please help!

In reply to ss ss

Re: Programmatically adding file modules

by Anthony Aoun -

I am also trying the same thing,

I need to programmatically add records into the table mdl_files.

I figured out that I need to link it to a course before and to the following tables 

(mdl_course_sections , mdl_course_modules, mdl_context, mdl_files, mdl_folder)

and I was able to display the file on moodle under the corresponding course, 

I even encrypted the pathnamehash with sha1 and added the file to a folder under moodle directory

But I am receiving an error that the file does not exists when I click on it.

And I don't know what I should fill in the contenthash(The sha1 hash of content).

Am I missing a table or a step ?

Can anyone help ?

In reply to Anthony Aoun

Re: Programmatically adding file modules

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

As mentioned above, you should never try to add records directly to the mdl_files table - it will almost certainly not work.

Always use the Moodle file API to do this (it will fill in the contenthash for you and make sure the file is stored in the correct location).


In reply to Davo Smith

Re: Programmatically adding file modules

by Anthony Aoun -

I am facing problems when using Moodle file API :

I have a PHP script that check if the file exists and use:

 if ($fs->file_exists($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) 

...

and $fs->create_file_from_pathname($fileinfo, $source_filename);

...

and this is always returning false on file names containg Arabic language (I tried to encode/decode the filename to utf8...), but nothing seems to work.

My files are stored under windows.

I am running this script via cmd and the arabic letters in filenames are displayed as ?? when echo

In reply to Anthony Aoun

Re: Programmatically adding file modules

by Darko Miletić -

If you are saying that you have a file on your windows server with name that contains unicode characters I will have to dissapoint you. AFAIK PHP does not support that use case under windows. Not sure for PHP 7 but earlier versions behaved that way.

See here for more details

http://stackoverflow.com/questions/977635/how-to-open-file-in-php-that-has-unicode-characters-in-its-name

In reply to Darko Miletić

Re: Programmatically adding file modules

by Anthony Aoun -
That's why I am trying to insert directly into moodle database.

I have been trying to use the PHP plugins and encodings but they don't work.

Is there a way to do the file upload via mysql ?

Thanks