Generate Path for the image.

Generate Path for the image.

by Hossein Poursaeedi -
Number of replies: 19

i upload image in my  plugin admin setting with :

admin_setting_configstoredfile
 
i need this uploaded image to use in my plugin and to create the path for it , i follow :
$context = context_system::instance();
$fs = get_file_storage();
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, my_plugin, my_area, false, '', false);
$file = reset($files);

$url = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
);

it will generate the path with my image name as i gave it during upload time, but when i use it in my plugin, notching shows.
during use it in url , it gives me "Error code: filenotfound" .
may i know your suggestion ?
if its depend on permission  and i need to show this image in public ,what i can do?

thanks
Average of ratings: -
In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi houssein, it sounds like you're still working on the same thing as in your other thread.  Did my reply there help you at all?

In reply to Mark Johnson

Re: Generate Path for the image.

by Hossein Poursaeedi -

Hello Mark,
yup i read that and thank you for your reply , but the issue is, i know the name of the file and config data, my issue is generating path for it, lf you see my post here, i clearly mention what i did, but still can get the picture 

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

So was I incorrect in suggesting that calling get_config to get the setting used by admin_setting_configstoredfile would give you the path for the file?  Can you post what it returns instead?

In reply to Mark Johnson

Re: Generate Path for the image.

by Hossein Poursaeedi -

no , "get_config " only gives configuration data which store in config plugin , in db, and only store name of the file.
for path , need to generate it in Moodle and i have issue with  it 

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ok, well it looks like you're along the right lines, if you post the actual line of code used to display the admin_setting_configstoredfile field, it would help in working out why the code you've posted above doesn't work.


In reply to Mark Johnson

Re: Generate Path for the image.

by Hossein Poursaeedi -
this is setting part

$settings->add(new admin_setting_configstoredfile(
'my_plugin/my_name'
get_string('file', 'my_plugin'),
get_string('fileDes', 'my_plugin'),
'my_area'
));
In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Try something like this, I think you might have gotten some of your arguments wrong:

$files = $fs->get_area_files($context->id, 'my_plugin', 'my_area', 0);
$file = reset($file);
$url = make_pluginfile_url($context->id, 'my_plugin', 'my_area', 0, $file->get_filepath(), $file->get_filename());
In reply to Mark Johnson

Re: Generate Path for the image.

by Hossein Poursaeedi -

Thanks for your reply Mark,

it has same result of mine , i can get the path , but it gives error "Sorry, the requested file could not be found"
and even in image tag, cant show it.

is it any permission issu from moodleData folder?
as i know we store files in moodle data and make path in local plugin ?

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Hossein Poursaeedi -

beside that, the item Id for picture is always 0 in database , is it normal?

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You can specify the itemid as an optional 5th parameter for admin_setting_configstoredfile().  If you don't specify, it uses 0.

Can you do print_object($file), and post the output?

In reply to Mark Johnson

Re: Generate Path for the image.

by Hossein Poursaeedi -

Array
(
    [f95256e9a98dabad8a7a1a6e7f766f2cbe65bd20] => stored_file Object
        (
            [fs:stored_file:private] => file_storage Object
                (
                    [tempdir:file_storage:private] => C:\xampp\htdocs\MOODLE\moodledata/temp/filestorage
                    [filesystem:file_storage:private] => file_system_filedir Object
                        (
                            [filedir:protected] => C:\xampp\htdocs\MOODLE\moodledata/filedir
                            [trashdir:protected] => C:\xampp\htdocs\MOODLE\moodledata/trashdir
                            [dirpermissions:protected] => 511
                            [filepermissions:protected] => 438
                        )

                )

            [file_record:stored_file:private] => stdClass Object
                (
                    [id] => 167
                    [contenthash] => ea852f03c19e4cddfa896582c960da33f15c6ebe
                    [pathnamehash] => f95256e9a98dabad8a7a1a6e7f766f2cbe65bd20
                    [contextid] => 1
                    [component] => my_plugin
                    [filearea] => my_area
                    [itemid] => 0
                    [filepath] => /
                    [filename] => gol.jpg
                    [userid] => 
                    [filesize] => 152437
                    [mimetype] => image/jpeg
                    [status] => 0
                    [source] => 1920x1080.jpg
                    [author] => Hossein PS
                    [license] => allrightsreserved
                    [timecreated] => 1516368864
                    [timemodified] => 1516368867
                    [sortorder] => 0
                    [referencefileid] => 
                    [repositoryid] => 
                    [reference] => 
                    [referencelastsync] => 
                )

            [repository:stored_file:private] => 
            [filesystem:stored_file:private] => file_system_filedir Object
                (
                    [filedir:protected] => C:\xampp\htdocs\MOODLE\moodledata/filedir
                    [trashdir:protected] => C:\xampp\htdocs\MOODLE\moodledata/trashdir
                    [dirpermissions:protected] => 511
                    [filepermissions:protected] => 438
                )

        )

)

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ah, I just realised what's probably wrong.

pluginfile.php doesn't serve the file for you - it calls my_plugin_pluginfile() from your plugin's lib.php file.  You'll need to define this function - look at some existing plugins for examples.

In your case you probably just need to reconstruct the path from the function arguments, do $file = $fs->get_file_by_hash(sha1($path)); then send_stored_file($file);

In reply to Mark Johnson

Re: Generate Path for the image.

by Hossein Poursaeedi -

Thanks for your reply Mark,
when i check different libs , i saw all using "send_stored_file" which internally use "send_file".
"send_file" make images headers to show it. but the issue is , in my plugin, i have many different images which i need to call (iv separate it by area) and i really didn't see in any libs call  pluginfile() .
let say in  template , moodle use "/theme/image.php/" to generate mage , is there any other image generator file like this which i pass path and get the images and print it out url in variable ?? (same as moodle template)


thanks

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Darko Miletić -

Here is a fully working example of a plugin that serves it's own files

https://github.com/kiklop74/moodle-local_fooplug


It does contain it's own _pluginfile method.



In reply to Darko Miletić

Re: Generate Path for the image.

by Darko Miletić -

I also added example how to actually use the stored setting. See index.php.

In reply to Darko Miletić

Re: Generate Path for the image.

by Hossein Poursaeedi -

Dear Darko ,
thanks for your nice job.
I do it same in my block lib and setting files and call it instead of setting , in some classes of my own block  . the issue is it generate the correct path and file name ,but unfortunately i receive "request file could not be found ".


ps. it generate bellow file :
<img src="http://localhost/MOODLE/moodle/pluginfile.php/1/my_plugin/my_area/0/apple.jpg" alt="This is my image!!!" />

Attachment error.png
In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Darko Miletić -

This ping-pong is pointless. If you want real help we need to see the complete code of your plugin since obviously you are doing something wrong. Upload it to github/bitbucket and let us see.

In reply to Darko Miletić

Re: Generate Path for the image.

by Hossein Poursaeedi -

Thanks Darko, i found the issue .
it was because of filearea , when i use many "configstoredfile" in same setting pages.


thanks alot.

In reply to Hossein Poursaeedi

Re: Generate Path for the image.

by Elias Espino -

Hi Hossein , i have the same problem

can you explait better  what is the solution.

thanks !