Hi
is it possible to upload an entire folder (which contains several files, organized in sub and sub-sub folders) through the panel? I’m using FlippingBook which turn a single pdf into several files organized in a defined structure of folder and subfolders, to let users browse html5 files instead of pdf.
thankx
Well, I guess you have already tried but failed. So the answer is “No”, at least not via the file uploader.
You might be able to achieve this with a custom upload field instead of using the file uploader.
Maybe you can upload a zip file and unzip via a hook.
Reading through the toolkit I’m pretty sure I’ve seen somewhere that Kirby has a function to unzip files to a directory. I could be wrong but I’m sure I’ve seen it in the past.
If it does have this, upload a zip using a custom uploader and call this function to unzip the files.
@MinmlCo Yes, in the toolkit’s file class:
public static function unzip($file, $to) {
if(!class_exists('ZipArchive')) {
throw new Exception('The ZipArchive class is not available');
}
$zip = new ZipArchive;
if($zip->open($file) === true) {
$zip->extractTo($to);
$zip->close();
return true;
} else {
return false;
}
}
Your hook would then look as simple as this:
kirby()->hook('panel.file.upload', function($file) {
if($file->is('zip')) {
f::unzip($file, $file->dir());
}
});
Edit: if you want, you can delete the original zip file after unzipping within the hook, or do it manually.
1 Like
I knew I’d seen it before. Such a simple solution @texnixe!
@MinmlCo, @texnixe, thank for your answers.
Unfortunately, the hook works when the zip contains just files, but if contains also folders, it doesn’t work.
For example, if the zip contains an organized structure of folder and files just like this:
Folder 1
- File A
– Folder 1.1
— File B
the zip file is uploaded but it is not unzipped. Any more hints?
thank you
In my tests it worked, otherwise I wouldn’t have removed “don’t know if it works” from my original answer (only tested on localhost, though). But it may in fact depend on several factors, e.g. the size of the contained folders in conjunction with your php.ini settings. I haven’t looked into the details, but my tests failed with folders that contained too many files or too big files. I’d suggest you make sure that the typical PHP settings for script execution, post_max_size etc. are set high enough.
Also, have you checked your folder structure to see if the folders are really not there or only not shown in the Panel because of blueprint settings (asking just in case
)?
Oooops… @texnixe you are sooo right. It works like a charm… The blueprint settings didn’t allow me to see the folders created. Thank you!