0

I'm trying to create a fallback method when ZipArchive is not present. I'm seeking to use the _unzip_file_pclzip() function contained in wp-admin/includes/file.php

However, I'm not sure what is expected for the $needed_dirs argument.

My target folder for the zip will be the "styles" folder under my theme folder, so the path would be "wp-content/themes/mytheme/styles/"

Is that what's expected for $needed_dirs?

function openZip($file_to_open) { 
    global $wp_filesystem;//required if _unzip_file_pclzip is used directly
    global $target;  
    if(class_exists('ZipArchive'))
    {
        $zip = new ZipArchive();  
        $x = $zip->open($file_to_open);  
        if($x === true) 
        {  
            $zip->extractTo($target);  
            $zip->close();                
            unlink($file_to_open);  
        } else {  
            die("There was a problem. Please try again!");  
        }
    }
    else
    {
        $needed_dirs = array();
        _unzip_file_pclzip($file_to_open, $target, $needed_dirs);
    }
} 

1 Answer 1

1

@Scott B I have not tested this script, taken from inlcudes/file.php -> line 559

_unzip_file_pclzip assumes WP_Filesystem() has already been called, so you need to set up global $wp_filesystem

global $wp_filesystem;
$needed_dirs = array();
$target = trailingslashit($target);

// Determine any parent dir's needed (of the upgrade directory)
if ( ! $wp_filesystem->is_dir($target) ) { //Only do parents if no children exist
    $path = preg_split('![/\\\]!', untrailingslashit($target));
    for ( $i = count($path); $i >= 0; $i-- ) {
        if ( empty($path[$i]) )
            continue;

        $dir = implode('/', array_slice($path, 0, $i+1) );
        if ( preg_match('!^[a-z]:$!i', $dir) ) // Skip it if it looks like a Windows Drive letter.
            continue;

        if ( ! $wp_filesystem->is_dir($dir) )
            $needed_dirs[] = $dir;
        else
             break; // A folder exists, therefor, we dont need the check the levels below this
    }
}
3
  • I've added the global to my question code. I'm still not sure what to pass for $needed_dirs. Are you saying i can just pass an empty array?
    – Scott B
    Feb 1, 2011 at 16:42
  • I'm getting error > Call to a member function mkdir() on a non-object in C:\xampplite\htdocs\mysite\wp-admin\includes\file.php on line 703...Any ideas?
    – Scott B
    Feb 1, 2011 at 16:47
  • Best I can tell, the problem lies with the $wp_filesystem variable not getting indexed. When I var_dump($wp_filesystem), its returning NULL. So that's invalidating the call to $wp_filesystem->mkdir()
    – Scott B
    Feb 1, 2011 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.