File API doubts

File API doubts

by Juan Carlos Molina -
Number of replies: 2

Hello everybody,

I am working on a local module that once a course has a picture uploaded to the overview files area it creates a thumbnail and also a greyscale picture.

In order to get there I am first trying to get a copy of the already uploaded picture doing this:

$picture = $DB->get_record_select('files',"contextid = $context_id->id and filearea = 'overviewfiles' ,null, 'id,contenthash,pathnamehash,filename');
$fs = get_file_storage();

$filerecord = array('contextid'=>$system_context, 'component'=>'local_image', 'filearea'=>'thumbnail',
'itemid'=>0, 'filepath'=>'/', 'filename'=>$picture->filename,
'timecreated'=>time(), 'timemodified'=>time());
$original_picture = $fs->get_file_by_hash($picture->pathnamehash);
$stored = $fs->create_file_from_storedfile($filerecord,$picture->id);
This is creating an entry like this one in the database:

id contenthash  pathnamehash contextid component filearea itemid filepath filename userid filesize mimetype status source author license timecreated timemodified sortorder referencefileid
3519875 2ea0e8ab9d835a0e0a04dd8e46266f510312c2df  8f45605131ba6f3a6247bb35148c80acc6a95625 87418 course overviewfiles 0 / 71ZIsWyQspL._SL1050_.jpg 2 121931 image/jpeg 0 71ZIsWyQspL._SL1050_.jpg Juan Carlos Giménez allrightsreserved 1563354618 1563354621 0
3519924 2ea0e8ab9d835a0e0a04dd8e46266f510312c2df  20ac9a4b6523e634e867b28122cf627564d8f981 1 local_image thumbnail 0 / 71ZIsWyQspL._SL1050_.jpg 121931 image/jpeg 0 71ZIsWyQspL._SL1050_.jpg 1563362139 1563362139 0
When I access the url: pluginfile.php/87418/course/overviewfiles/71ZIsWyQspL._SL1050_.jpg

I can see perfectly the picture

When I access the url:

pluginfile.php/1/local_image/thumbnail/71ZIsWyQspL._SL1050_.jpg

I get a code 200 and the browser shows a broken file as a result.

Can somebody give me a hand to know where is my error?
Average of ratings: -
In reply to Juan Carlos Molina

Re: File API doubts

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi,

maybe in your pluginfile function is an unwanted output, some notice or another error. So the best would be to check the output if you try to get the file and take a look with an editor. If there is an error in it you should see it just on top of the file content.

Best regards
Andreas

In reply to Juan Carlos Molina

Re: File API doubts

by A. Obeid -
Try this
//$context = context_module::instance($cm->id);
$path = '/' . $context->id . '/course/overviewfiles/0'. $file->get_filepath() . $file->get_filename();
$fullurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $path, false);
Average of ratings: Useful (1)