I'm trying to create a CSV file that can be downloaded by the user.
I've tried to follow the API documents and get most of it working. Here's my code:
$context = context_course::instance($COURSE->id);
$fs = get_file_storage(); // Prepare file record object
$fileinfo = array(
'contextid' => $context->id, // ID of context
'component' => 'block_voucher', // usually = table name
'filearea' => 'voucher', // usually = table name
'itemid' => $timestamp, // usually = ID of row in table
'filepath' => '/', // any path beginning and ending in /
'filename' => $filename); // any filename
// Create file containing text
$fs->create_file_from_string($fileinfo, $stringtowrite); //String to write is the CSV contents
$url = moodle_url::make_pluginfile_url($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);
echo "<a href='".$url."'>Click to download</a>";
Clicking the link gives errors, I tried using... http://docs.moodle.org/dev/File_API#Serving_files_to_users
but I get the errors:
Error code: filenotfound
- line 463 of /lib/setuplib.php: moodle_exception thrown
- line 1948 of /lib/filelib.php: call to print_error()
- line 4610 of /lib/filelib.php: call to send_file_not_found()
- line 37 of /pluginfile.php: call to file_pluginfile()
I cannot find the file as a "real" file anywhere on the server although I am assuming it is stored in the database.
Cheers
Dave