save image in database ???

save image in database ???

by Nilesh Pathade -
Number of replies: 7

Hi All,

Anyone please help me. 

I am developing simple local plugin which store the image by admin and showing to another page same image.

I used following code. 

$mform->addElement('filepicker', 'uploadfile', get_string('file'), null, array('accepted_types' => array('jpg', 'png')));

After submiited form i have print 

 if ($fromform = $mform->get_data()) {
    print_r($fromform->uploadfile);
}

It print digit like this : 473708135

please let me know how to store  image file ??


Average of ratings: -
In reply to Nilesh Pathade

Re: save image in database ???

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Although it's not impossible, you don't normally store a file (of any kind) in a database. Certainly not in Moodle. There's a whole file system for this.

As this refers to a form, you probably want - https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms

Although the following is useful as well - https://docs.moodle.org/dev/File_API

Note that the file is stored with an 'item id'. That's what you want to store in the database so you can reference the file. However, there is often a 'natural' id that you can use (e.g. the id number of a database record you will be creating anyway)
Average of ratings: Useful (1)
In reply to Howard Miller

Re: save image in database ???

by Nilesh Pathade -
Thank you for reply.

Can any example to store image and display it on web page.

I have use this one : https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Using_the_filepicker_element

what is it ??
$storedfile = $mform->save_stored_file('userfile', ...);

is it store file into mdl_file table ???

Please can you share simple example to store image and display it??
In reply to Nilesh Pathade

Re: save image in database ???

by Mayank Tyagi -
Hi Nilesh,

The code "print_r($fromform->uploadfile);" gives you the itemid of the stored file. Their are many way  available both in moodle and php to print that image on page. You just need to pass correct parameter like file context, itemid, filepath, filename. All these information are available in prefix_files. You can find that file in moodle and move that file in temporary location and then print it by using moodle core function or simple php functions.
To store files you can use Moodle filepicker or Filemanager  :-
                        $filemanageroptions = array();
			$filemanageroptions['accepted_types'] = '*.pdf, *.jpg';
			$filemanageroptions['maxbytes']       = $CFG->maxbytes;
			$filemanageroptions['subdirs']        = 0;
			$filemanageroptions['maxfiles']       = 1;
$storeFile->addElement('filemanager', 'attachments', get_string('file_upload', 'local_plugin'), null, $filemanageroptions);


In reply to Mayank Tyagi

Re: save image in database ???

by Nilesh Pathade -
Thank you Mayank for reply.

I have used same code for store the image file for my local plugin.

and mdl_file inserted 2 records which i have attached.

and when I call following URL then it show me image not found.

Please assist where I am going to wrong.

Attachment err.png
Attachment mdl_file.png
In reply to Nilesh Pathade

Re: save image in database ???

by Mayank Tyagi -
Hi Nilesh,

Can you please clear me why are you hitting this url. This error generally thrown by "send_file_not_found". In background this url calls "file_pluginfile($relativepath, $forcedownload, $preview, $offline, $embed)". What is the purpose of using this url. Sometime we need to call this url like "pluginfile.php/contextid/component/filearea/sortorder/filename" (I'm not sure :P).
In reply to Mayank Tyagi

Re: save image in database ???

by Nilesh Pathade -
If I am wrong then correct me.

Just assume I am using tag of HTML. and scr attribute required URL which gives me image path.

In my case following url should be give me image path.

http://localhost/moodle381/pluginfile.php/5/user/draft/307715000/moodle-logo.jpg

As we taking last post relation.
There is contextid = 5, component=user, filearea = draft, and itemid = 307715000 and filename is moodle-logo.jpg

I have try same with course image and when I put all this into same URL it showing me Image.
In reply to Nilesh Pathade

Re: save image in database ???

by Mayank Tyagi -
Hi Nilesh,

Yes you are right. Moodle uses this url to show images. Try to use below script. I have used it to show files like pdf, word docs. May be it will help. Otherwise 
we need to move your file at temporary location and then use this temporary location to show file.

$fileRecord = $DB->get_record("files", array("id" => $id));

$fs = get_file_storage();
 
// Prepare file record object


$fileFlag = false;

if($fileRecord) {

    $fileinfo = array(
        'component'  => $fileRecord->component,     // usually = table name
        'filearea'   => $fileRecord->filearea,     // usually = table name
        'itemid'     => $fileRecord->itemid,               // usually = ID of row in table
        'contextid'  => $fileRecord->contextid, // ID of context
        'filepath'   => $fileRecord->filepath,           // any path beginning and ending in /
        'filename'   => $fileRecord->filename    // any filename
        ); 
        

     
    // Get file
    $file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);

    if($file) {
        $fileFlag = true;
    }
                      
}
// If file exists print file
if ($fileFlag) {
    
    echo send_stored_file($file, 60*60);
    
} else {
    // file doesn't exist - do something
    send_file_not_found();
}