Jump to content

$files->unzip nested folders?


Spica
 Share

Recommended Posts

  • 3 weeks later...

So, yes, this method unzips all nested files.

My use case, for anyone who may need:

<?
// hook into tutorial upload and unzip file into destination
wire()->addHookAfter("InputfieldFile::processInputFile", function(HookEvent $event)  {
    $pagefile = $event->argumentsByName('pagefile');
    // limit to a specific field {file_tutorial}, restricted to zip uploads
    if($pagefile->field->name != 'file_tutorial') return;
	// full disk path to your custom uploads directory
    $tutorialsBaseDirectory = $this->wire('config')->paths->assets . 'tutorials/';
	// extend path with custom directory nam corresponding to page id
    $pageIdDirectory = $tutorialsBaseDirectory . $pagefile->page->id . "/";
    // extend path with custom directory name from the filenme without extention
    $tutorialDirectory = $pageIdDirectory . $pagefile->basename($ext = false);
    // use ProcessWire's $files API
    // @see: http://processwire.com/api/ref/files/
    $files = $this->wire('files');
    // Remove directory and its files after ensuring $pathname is somewhere within /site/assets/
	$files->rmdir($pageIdDirectory, true, [ 'limitPath' => $tutorialsBaseDirectory ]);
    // make tutorials id directory correspondig to ressources id
    if($files->mkdir($tutorialDirectory, true)) {
	   // directory created: /site/assets/tutorials/id/filenameWithoutExtension
	}
    // get zipfile and destination and unzip
	$zip = $pagefile->filename;
	$dst = $tutorialDirectory;
	$items = $files->unzip($zip, $dst);
});

Adopted from 

 

The script takes an uploaded zip and unzips it into the destination folder. As it has an index.html I can point a link to it.

 

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...