I have an instance of an activity and when I want to view it I want to extract the associated zip file to be extracted and set the index.html file inside to be the starting page inside view.php.
I found out that the filepath I passed in the extract function doesn’t exist, but I can access its fields in the database.
How do I properly pass in the filepath parameter?
Edit:
$fp = get_file_packer('application/zip'); $fileinfo = array( 'component' => 'mod_game', 'filearea' => 'content', 'itemid' => 0, 'contextid' => 472, 'filepath' => '/', 'filename' => 'game.7z'); $myfile = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); $filepath = '/'.$context->id.'/mod_game/content/'.$game->revision.$myfile->get_filepath().$myfile->get_filename(); $files = $fp->extract_to_pathname($filepath, $CFG->dirroot.'/mod/game/games'.$filepath.'_extracted');
Advertisement
Answer
I can see a number of issues here.
Firstly, the file ending (.7z) suggests that this is not a Zip file, but 7Zip file, which Moodle does not (as far as I am aware) support the unzipping of.
The next problem is that you don’t appear to have created the folder into which you want to extract the files – you need to create the destination folder, before you try to extract files into it.
Finally, you appear to be trying to pass the (partial?) URL of the file to unzip, rather than either the path to it on the server (NOT recommended in this case) or the stored_file instance itself (strongly recommended).
Unless you need these long term, I would suggest a better solution would look like this:
// Make a temporary folder that will be automatically deleted at the end of the request. $dest = make_request_directory(); // Extract the stored_file instance into this destination. $files = $fp->extract_to_pathname($myfile, $dest);