diff --git a/lib/filestorage/zip_archive.php b/lib/filestorage/zip_archive.php index f22a28c89f500..435660ecd9471 100644 --- a/lib/filestorage/zip_archive.php +++ b/lib/filestorage/zip_archive.php @@ -361,7 +361,7 @@ public function add_directory($localname) { if (!isset($this->za)) { return false; } - $localname = ltrim($localname, '/'). '/'; + $localname = trim($localname, '/'). '/'; $localname = $this->mangle_pathname($localname); if ($localname === '/') { @@ -369,10 +369,12 @@ public function add_directory($localname) { return false; } - if (!$this->za->addEmptyDir($localname)) { - return false; + if ($localname !== '') { + if (!$this->za->addEmptyDir($localname)) { + return false; + } + $this->modified = true; } - $this->modified = true; return true; } diff --git a/repository/draftfiles_ajax.php b/repository/draftfiles_ajax.php index 43ff97d678c2f..904c5dc76337e 100644 --- a/repository/draftfiles_ajax.php +++ b/repository/draftfiles_ajax.php @@ -219,8 +219,11 @@ $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); $parent_path = $file->get_parent_directory()->get_filepath(); + + $filepath = explode('/', trim($file->get_filepath(), '/')); + $filepath = array_pop($filepath); - if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) { + if ($newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) { $return = new stdClass(); $return->filepath = $parent_path; echo json_encode($return); @@ -251,7 +254,7 @@ // archive compressed file to an unused draft area $newdraftitemid = file_get_unused_draft_itemid(); - if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) { + if ($newfile = $zipper->archive_to_storage(array('/' => $stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) { $return = new stdClass(); $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out(); $return->filepath = $parent_path; diff --git a/repository/draftfiles_manager.php b/repository/draftfiles_manager.php index 28e585f86b2ac..d84f39b14e499 100644 --- a/repository/draftfiles_manager.php +++ b/repository/draftfiles_manager.php @@ -147,7 +147,7 @@ $filename = get_string('files').'.zip'; } - if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) { + if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) { $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out(); header('Location: ' . $fileurl ); } else { @@ -161,14 +161,16 @@ $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if (!$file->get_parent_directory()) { $parent_path = '/'; + $filepath = '/'; $filename = get_string('files').'.zip'; } else { $parent_path = $file->get_parent_directory()->get_filepath(); $filepath = explode('/', trim($file->get_filepath(), '/')); - $filename = array_pop($filepath).'.zip'; + $filepath = array_pop($filepath); + $filename = $filepath.'.zip'; } - $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id); + $newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id); $home_url->param('action', 'browse'); $home_url->param('draftpath', $parent_path);