Exception - Object of class stored_file could not be converted to string

Exception - Object of class stored_file could not be converted to string

by Marinda Faasen -
Number of replies: 11

Hi

I need help please. I can recite everything on the filemanager found on Moodle by heart, (and on google), but I can't get it to work.

I used the filemanager to get a user to upload a file. It is part of a block I am developing. That seems to be successful. 

The following code creates a link so the file can be downloaded.

$fs = get_file_storage();

    $files = $fs->get_area_files( $context->id, 'block_extensions',  'evidencefile', $itemid,  '',  false);

foreach ($files as $file) {

          // If the record is a directory, skip.

            if ($file->is_directory() and $file->get_filename() == '.') {

                continue;

         }

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

    $link = html_writer::link($url,$file->get_filename());

    $filename = $file->get_filename();

}

And finally the plugin file:

function block_extensions_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = []) {

    if ($context->contextlevel != CONTEXT_COURSE) {

        send_file_not_found();

    }

    if ($filearea !== 'evidencefile') {

         send_file_not_found();

    }

    require_course_login($course, true, $cm);

   $itemid = array_shift($args); // The first item in the $args array.

   $filename = array_pop($args); // The last item in the $args array.

    if (!$args) {

        $filepath = '/'; // $args is empty => the path is '/'

    } else {

        $filepath = '/' . implode('/', $args) . '/'; // $args contains elements of the filepath

    }

    // Retrieve the file from the Files API.

    $fs = get_file_storage();

    $file = $fs->get_file($context->id, 'block_extensions', $filearea, $itemid, $filepath, $filename);

    if (!$file) {

      send_file_not_found();

    }

   send_stored_file($file, 0, 0, $forcedownload, $options);

}

But,  I get the Exception - Object of class stored_file could not be converted to string error, when I click on the file link. 

Please help.

Average of ratings: -
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi,

Check your $itemid - I think if it is not set - then it is not passed in the $args array - do you have an item id set when the file is uploaded??
In reply to Benjamin Ellis

Re: Exception - Object of class stored_file could not be converted to string

by Marinda Faasen -
Hi
Thanks for the reply. I do have an itemid set when the file is uploaded. The student completes a form to request extension for an assignment. I link the id generated in the request table to the 'evidence file' that is submitted.
M
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi
I suspect something is wrong with your link as your pluginfile function appears good. Why not use $file = $fs->get_file() rather than $files = $fs->get_area_files() to get the specific file? Maybe something else is getting missed by the foreach $files loop. Otherwise I would add some debugging print_r() and die() to see what is creating the link.
In reply to Benjamin Ellis

Re: Exception - Object of class stored_file could not be converted to string

by Marinda Faasen -
This is the complete error message:
line 2250 of /lib/filelib.php: Error thrown
line 2528 of /lib/filelib.php: call to readfile_accel()
line 2673 of /lib/filelib.php: call to send_file()
line 202 of /blocks/extensions/lib.php: call to send_stored_file()
line 5028 of /lib/filelib.php: call to block_extensions_pluginfile()

Line 2250 of /lib/filelib.php: error_log('Non-empty default output handler buffer detected while serving the file ' . $file);

I tried using get_file(). It didn't make a difference. I can't remember now why. I might try it again.
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Benjamin Ellis -
Picture of Particularly helpful Moodlers

What version of Moodle are you working with?  In moodle 3.9 

Code extract of filelib.php for error.

which suggests there is something else in the output buffer - another file maybe???  Not sure that helps much.

In reply to Benjamin Ellis

Re: Exception - Object of class stored_file could not be converted to string

by Marinda Faasen -
So it is still not working. If I create a txt file with create_file_from_string i can use $file->get_content() and it displays the content of the file on the screen. If I use the pluginfile code to download the file, then it gives the same error. Either Object of class stored_file could not be converted to a string or if I force the download, then it simply says it couldn't find the web page. Error message: The webpage at https://.../pluginfile.php/1496/block_extensions/evidencefile/9/Database%20queries.pdf might be temporarily down or it may have moved permanently to a new web address. I am on Moodle vs 3.9.4
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Try renaming the file without a space in the name. Are other file uploads - file resources in courses - particularly pdfs working OK?
In reply to Benjamin Ellis

Re: Exception - Object of class stored_file could not be converted to string

by Marinda Faasen -
Other file uploads are working in the course and I do have a number of files that do not have spaces. I tested it on two installations. My localhost and on a life Moodle site.
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Weird. Try turning debugging off - maybe there is an unseen errors being sent before the file contents. Alternatively - use the Developer Tools in the browser and check what headers and content are being sent. I would even attempt cURL on the command line (as painful as that is) .
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Darko Miletić -
Most likely you have some output printed before serving the file. When sending file absolutely nothing must be printed by PHP before actual function is called. Probably an error or notice is displayed.
In reply to Marinda Faasen

Re: Exception - Object of class stored_file could not be converted to string

by Nadav Kavalerchik -
Picture of Core developers Picture of Plugin developers Picture of Testers Picture of Translators
I am posting a solution information to anyone installing unoconv with LibraOffice, and getting the same error message.
(This is not a direct solution to the above issue)

I had similar error message after trying to test the unoconv service I just installed on a RedHat 7.9 server.
I installed LibraOffice 7.3 directly from RPM packages and not used OS yum packages (ver 5.3)
So the libreoffice-pyuno was not found (bind) to the unoconv, and that is way I got a similar error to the one you got.
You will have to download find_uno.py from here to fix this: https://github.com/unoconv/unoconv#installing-unoconv
Average of ratings: Useful (1)