General developer forum

Creating a file from a /local/plugin.php

 
 
Andrew Normore
Creating a file from a /local/plugin.php
 

Hello again, I'm still having some trouble with images. 

I would like to use this code, to generate an image, but don't understand some of the concepts:

 

$fileinfo = array(

'contextid' => 1,
'component' => 'user', 
'filearea' => 'draft',
'itemid' => rand(5, 15),
'filepath' => '/', 
'filename' => $filename

); 

$fs->create_file_from_string($fileinfo, 'hello world');


 

I want this image to be avaliable from any where, even to public users. I can have it save to the database, but cannot retrieve it.

Why is it so complicated to create files in Moodle? Security? 

 

 

 
Average of ratings: -
Andrew Normore
Re: Creating a file from a /local/plugin.php
 

I was hoping I could just echo right after, and display the image

 

echo  "<img src='draftfile.php/1/user/draft/0/7/rptpietest_1_9.png' />";

 
Average of ratings: -
Davo
Re: Creating a file from a /local/plugin.php
Group DevelopersGroup Particularly helpful Moodlers

Assuming you are using the 'system' context for your image:

$context = context_system::instance();
$contextid = $context->id;
$component = 'local_mylocalplugin';
$filearea = 'anything_you_like'; // e.g. 'image'
$itemid = 0; 
$filepath = '/';

This identifies the context in which the file is stored, the plugin that is looking after it (so which should be used to check if the user has access to the file), the area for the files (e.g. in 'forum' there is an 'attachment' area for attachments to posts and a separate area, that I can't remember the name of, for image files embedded in the post itself), the itemid (for a forum, this is the ID of the post, so that attachments to different posts are kept separate). Then there is the filepath (so that you can have subfolders within a file area) and the filename (which I hope needs no explanation).

To create a link to the file (after you have called 'create_file_from_string', use:
moodle_url::make_pluginfile_url($contextid, $component, $filearea, $itemid, $filepath, $filename);

This will then look for the file local/mylocalplugin/lib.php and expect to find a function called 'local_mylocalplugin_pluginfile()' (take a look at the 'forum_pluginfile()' function in mod/forum/lib.php for an example of this function).

Why is it so complicated? Two reasons:

  1. Every file is associated with a plugin, so the plugin can decide which file are actually needed during backup (so you don't have to include every possible course file, when only backing up one activity)
  2. Only individual plugins can decide which users are allowed to access each file (e.g. teachers can access the assignment submissions for all students in the class, only enrolled students can access the files in a course), so the URL to the file has to include enough information for the plugin to make that decision.

 

 
Average of ratings: -
Andrew Normore
Re: Creating a file from a /local/plugin.php
 

Davo, thanks for your excellent level of assistance over the last few days. 

Can't I just use file.php or draftfile.php to call this image? It doesn't seem to be working, and am simply curious as to why it fails?

Why do I get 2 records?

y

 
Average of ratings: -
Andrew Normore
Re: Creating a file from a /local/plugin.php
 

Here's the code I'm running, it doesn't find the file. 

 

$context = context_system::instance();
$fs = get_file_storage();
$itemid = rand(1,1500);

// Prepare file record object
$fileinfo = array(
'contextid' => $context->id,
'component' => 'local_webinars',
'filearea' => 'dashboard',
'itemid' => $itemid,
'filepath' => '/',
'filename' => $filename);

// Create file containing text 'hello world'
$fileRecord = $fs->create_file_from_string($fileinfo, 'hello world');

$url = "$CFG->wwwroot/pluginfile.php/$context->id/local_webinars/dashboard/$itemid/$filename";
echo "<img src='$url' alt='NOTFOUND' />";
die;

 
Average of ratings: -
Davo
Re: Creating a file from a /local/plugin.php
Group DevelopersGroup Particularly helpful Moodlers

What does you local_webinars_pluginfile function (in local/webinars/lib.php) contain?

What happens when you add some debugging output to that function?

 
Average of ratings: -
Davo
Re: Creating a file from a /local/plugin.php
Group DevelopersGroup Particularly helpful Moodlers

Draft file is for retrieving files that are temporarially stored in the draft area whilst a file is being edited. This allows, for example, uploaded images to immediately appear in the text editor, before they have been saved into the 'real' area.

Files in the draft file area are automatically deleted after a day or so.

 
Average of ratings: -
Andrew Normore
Re: Creating a file from a /local/plugin.php
 

Thanks for the explination on draftfile.php

Here's the complete code:

 

 

 

$context = context_system::instance();
var_dump ($context);

$fs = get_file_storage();
$itemid = rand(1,1500);

// Prepare file record object
$fileinfo = array(
'contextid' => $context->id,
'component' => 'local_webinars',
'filearea' => 'dashboard',
'itemid' => $itemid,
'filepath' => '/',
'filename' => $filename);

// Create file containing text 'hello world'
$fileRecord = $fs->create_file_from_string($fileinfo, $file);

$url = moodle_url::make_pluginfile_url($context->id, 'local_webinars', 'dashboard', $itemid, '/', $filename);

echo "<img src='$url' alt='NOTFOUND' />";

 

 

 

 

 

function webinars_pluginfile($context, $component, $filearea, $itemid, $path, $filename) {

global $CFG, $DB, $USER;

if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}

require_once($CFG->libdir.'/filelib.php');

$fullpath = "/{$context->id}/$component/$filearea/$itemid/$path/$filename";

$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, true); // download MUST be forced - security!

}

 
Average of ratings: -
Davo
Re: Creating a file from a /local/plugin.php
Group DevelopersGroup Particularly helpful Moodlers
Try: function local_webinars_pluginfile(... And the contextlevel will be CONTEXT_SYSTEM
 
Average of ratings: -
Picture of Jitendra Gaur
Re: Creating a file from a /local/plugin.php
 

use this structure in for local lib file -

function local_pluginname_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
    global $CFG, $DB, $USER;

    $fullpath = "/{$context->id}/local_pluginname/$filearea/{$args[0]}/{$args[1]}";

    $fs = get_file_storage();
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
        return false;
    }

    send_stored_file($file, 0, 0, true); // download MUST be forced - security!
}

 

 
Average of ratings: -
Andrew Normore
Re: Creating a file from a /local/plugin.php
 

This is the result of a var_dump($url);

 

object(moodle_url)[4308]
  protected 'scheme' => 

string

 'http' (length=4)
  protected 'host' => 

string

 'rts.dev' (length=7)
  protected 'port' => 

string

 '' (length=0)
  protected 'user' => 

string

 '' (length=0)
  protected 'pass' => 

string

 '' (length=0)
  protected 'path' => 

string

 '/pluginfile.php' (length=15)
  protected 'slashargument' => 

string

 '/1/local_webinars/dashboard/1202/rptpietest_1_9.png' (length=51)
  protected 'anchor' => null
  protected 'params' => 
    array (size=0)
      empty
 
Average of ratings: -
Andrew Normore
Re: Creating a file from a /local/plugin.php
 

I have the file reading from the database, but it won't display:

 

object(stored_file)[4303]
  private 'fs' => 
    object(file_storage)[4306]
      private 'filedir' => 

string

 'E:\wamp\www2\svn/filedir' (length=24)
      private 'trashdir' => 

string

 'E:\wamp\www2\svn/trashdir' (length=25)
      private 'tempdir' => 

string

 'E:\wamp\www2\svn\temp/filestorage' (length=33)
      private 'dirpermissions' => 

int

 511
      private 'filepermissions' => 

int

 438
  private 'file_record' => 
    object(stdClass)[4329]
      public 'id' => 

string

 '905' (length=3)
      public 'contenthash' => 

string

 'd0ac9c0148d4cdd9b751dcea736b6aa51f22e35a' (length=40)
      public 'pathnamehash' => 

string

 'c1a2806a978fbf6af824e4124e191cffa8895b4b' (length=40)
      public 'contextid' => 

string

 '1' (length=1)
      public 'component' => 

string

 'local_webinars' (length=14)
      public 'filearea' => 

string

 'dashboard' (length=9)
      public 'itemid' => 

string

 '63341' (length=5)
      public 'filepath' => 

string

 '/' (length=1)
      public 'filename' => 

string

 'rptpietest_1_9.png' (length=18)
      public 'userid' => null
      public 'filesize' => 

string

 '73' (length=2)
      public 'mimetype' => 

string

 'image/png' (length=9)
      public 'status' => 

string

 '0' (length=1)
      public 'source' => null
      public 'author' => null
      public 'license' => null
      public 'timecreated' => 

string

 '1356111137' (length=10)
      public 'timemodified' => 

string

 '1356111137' (length=10)
      public 'sortorder' => 

string

 '0' (length=1)
      public 'referencefileid' => null
      public 'repositoryid' => null
      public 'reference' => null
      public 'referencelastsync' => null
      public 'referencelifetime' => null
  private 'filedir' => 

string

 'E:\wamp\www2\svn/filedir' (length=24)
  private 'repository' => null
 
Average of ratings: -
Picture of Abdul Bashet
Re: Creating a file from a /local/plugin.php
 

Hi Andrew Normore,

Have you solved this problem? Please provide me the exactly code to retrive the image. or how can I do <img src="image_path">

I found if just use the function send_stored_file($file, 0, 0, false); it is giving me the out put of file but not in moodle page.

Please help me.

 

 
Average of ratings: -
Picture of Jitendra Gaur
Re: Creating a file from a /local/plugin.php
 

Hi Abdul,

Use this code to show the image in img tag

$fullpath = "{$CFG->wwwroot}/pluginfile.php/{$file->get_contextid()}/local_yourpluginname/content/{$file->get_itemid()}/{$file->get_filename()}";
$url = "{$CFG->wwwroot}/pluginfile.php/{$file->get_contextid()}/local_yourpluginname/content/";
$filename = $file->get_filename();
$path = $url.$file->get_filepath().$file->get_itemid().'/'.$filename;
echo html_writer::tag('img','',array('src'=>$fullpath));

 
Average of ratings: -