Moodle

Sub folders in zip files are not being created

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Won't Fix
  • Affects Version/s: 1.8.9, 1.9.5
  • Fix Version/s: None
  • Component/s: Files API
  • Labels:
    None
  • Environment:
    All Plattaforms
  • Affected Branches:
    MOODLE_18_STABLE, MOODLE_19_STABLE

Description

Using unzip_file of compressed file with sub folders these sub foders were not move from temp folder (of dataroot) to the destination folder. The problem is with rename function in line 7575 of moodlelib.php

Activity

Hide
Petr Škoda (skodak) added a comment -

Hello,
I can not reproduce the problem ,works fine for me:
1/ create zip file with subdires
2/ upload to course
3/ unzip

Please note the target directory must be inside dataroot and it must already exist.

Show
Petr Škoda (skodak) added a comment - Hello, I can not reproduce the problem ,works fine for me: 1/ create zip file with subdires 2/ upload to course 3/ unzip Please note the target directory must be inside dataroot and it must already exist.
Hide
Tunya added a comment -

I put my module as example to show zip without subfolders

Show
Tunya added a comment - I put my module as example to show zip without subfolders
Hide
Petr Škoda (skodak) added a comment -

$pathzip = "$CFG->dirroot/mod/testunzip/template.zip";
$result = unzip_file ($pathzip, $destination, false);

You can not unzip into dirroot, you can unzip into dataroot only, sorry

Show
Petr Škoda (skodak) added a comment - $pathzip = "$CFG->dirroot/mod/testunzip/template.zip"; $result = unzip_file ($pathzip, $destination, false); You can not unzip into dirroot, you can unzip into dataroot only, sorry
Hide
Tunya added a comment -

But if you can not unzip into dirroot you can't unzip anything, because files on root folder are created (not subfolders).

Show
Tunya added a comment - But if you can not unzip into dirroot you can't unzip anything, because files on root folder are created (not subfolders).
Hide
Petr Škoda (skodak) added a comment - - edited

The only supported target for unzip operation is dataroot and its subdirectories, going to emphasise that in in-line docs.

Show
Petr Škoda (skodak) added a comment - - edited The only supported target for unzip operation is dataroot and its subdirectories, going to emphasise that in in-line docs.
Hide
Caroline Moore added a comment -

I was having this issue on my production and test servers, both running 1.9.5+. Additionally, when I used the backup and restore or import functionality, the database resources were being copied, but the files stored in the course folder in dataroot were not.

Behavior:

  • Upload a zip file with subfolders into a course's Files directory.
  • Click the Unzip link.
  • Moodle displays appropriate output, indicating that it's unzipping the file, but the files do not exist in the Files folder.

When I try the above with a zip file without subfolders, the files are unzipped and moved correctly.

I've poked in the code and determined that the files are being unzipped correctly into dataroot/temp/unzip/, but php fails when trying to move the files that are within subfolders because those subfolders do not exist within the destination directory.

When the function unzip_process_temp_dir encounters a subfolder, it simply calls itself recursively on the contents of the folder. It never actually creates the folder in the destination directory.

To resolve this problem, I added a line to the function unzip_process_temp_dir:

if (is_dir($fullfile)) { mkdir($descfile); // added to fix directory recursion problem // recurse into subdirs unzip_process_temp_dir($fullfile, $descfile); }

This fixed manual unzipping from inside the Files directory, and also fixed course import/restore functionality.

Show
Caroline Moore added a comment - I was having this issue on my production and test servers, both running 1.9.5+. Additionally, when I used the backup and restore or import functionality, the database resources were being copied, but the files stored in the course folder in dataroot were not. Behavior:
  • Upload a zip file with subfolders into a course's Files directory.
  • Click the Unzip link.
  • Moodle displays appropriate output, indicating that it's unzipping the file, but the files do not exist in the Files folder.
When I try the above with a zip file without subfolders, the files are unzipped and moved correctly. I've poked in the code and determined that the files are being unzipped correctly into dataroot/temp/unzip/, but php fails when trying to move the files that are within subfolders because those subfolders do not exist within the destination directory. When the function unzip_process_temp_dir encounters a subfolder, it simply calls itself recursively on the contents of the folder. It never actually creates the folder in the destination directory. To resolve this problem, I added a line to the function unzip_process_temp_dir: if (is_dir($fullfile)) { mkdir($descfile); // added to fix directory recursion problem // recurse into subdirs unzip_process_temp_dir($fullfile, $descfile); } This fixed manual unzipping from inside the Files directory, and also fixed course import/restore functionality.
Hide
Caroline Moore added a comment -

It looks like the formatting was lost on the code I copied into my comment above. Let's see if this looks any better:

In dirroot/lib/moodlelib.php, line 7573:

if (is_dir($fullfile)) {
mkdir($descfile); // added to fix directory recursion problem
// recurse into subdirs
unzip_process_temp_dir($fullfile, $descfile);
}

Show
Caroline Moore added a comment - It looks like the formatting was lost on the code I copied into my comment above. Let's see if this looks any better: In dirroot/lib/moodlelib.php, line 7573: if (is_dir($fullfile)) { mkdir($descfile); // added to fix directory recursion problem // recurse into subdirs unzip_process_temp_dir($fullfile, $descfile); }
Hide
Frederic Nevers added a comment -

Hi Caroline,

I have tried your fix and it works to some extent. The folders/subfolders are now created but the files which are part of those folders are not displayed (although Moodle reckons they are unzipped successfully). Basically, I end up with empty folders.

Do you happen to have a similar issue? I have tried unzipping the folder you have attached to this tracker. Is this how the moodlelib.php should have been modified?

$descfile = "$destpath/$cleanfile";
if (is_dir($fullfile))

{ mkdir($descfile); // added to fix directory recursion problem // recurse into subdirs unzip_process_temp_dir($fullfile, $descfile); }

if (is_file($fullfile)) {
// rename and move the file
if (file_exists($descfile)) { //override existing files unlink($descfile); }

Sorry for loss of formatting
Cheers, Fred

Show
Frederic Nevers added a comment - Hi Caroline, I have tried your fix and it works to some extent. The folders/subfolders are now created but the files which are part of those folders are not displayed (although Moodle reckons they are unzipped successfully). Basically, I end up with empty folders. Do you happen to have a similar issue? I have tried unzipping the folder you have attached to this tracker. Is this how the moodlelib.php should have been modified? $descfile = "$destpath/$cleanfile"; if (is_dir($fullfile)) { mkdir($descfile); // added to fix directory recursion problem // recurse into subdirs unzip_process_temp_dir($fullfile, $descfile); } if (is_file($fullfile)) { // rename and move the file if (file_exists($descfile)) { //override existing files unlink($descfile); } Sorry for loss of formatting Cheers, Fred
Hide
Marc Vorreiter added a comment -

we had the problem that only some files are not unzipt
we changed the rename into copy and unlink and now it works

Show
Marc Vorreiter added a comment - we had the problem that only some files are not unzipt we changed the rename into copy and unlink and now it works
Hide
Michael de Raadt added a comment -

Thanks for reporting this issue.

We have detected that this issue has been inactive for over a year has been recorded as affecting versions that are no longer supported.

If you believe that this issue is still relevant to current versions (2.1 and beyond), please comment on the issue. Issues left inactive for a further month will be closed.

Michael d;

lqjjLKA0p6

Show
Michael de Raadt added a comment - Thanks for reporting this issue. We have detected that this issue has been inactive for over a year has been recorded as affecting versions that are no longer supported. If you believe that this issue is still relevant to current versions (2.1 and beyond), please comment on the issue. Issues left inactive for a further month will be closed. Michael d; lqjjLKA0p6
Hide
Michael de Raadt added a comment -

I'm closing this issue as it appears to have become inactive and is probably not relevant to a current supported version. If you are encountering this problem or one similar, please launch a new issue.

Show
Michael de Raadt added a comment - I'm closing this issue as it appears to have become inactive and is probably not relevant to a current supported version. If you are encountering this problem or one similar, please launch a new issue.

People

Dates

  • Created:
    Updated:
    Resolved: