Jump to content
Spica

$files->unzip nested folders?

Recommended Posts

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...