Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
    }
} 
share|improve this question

1 Answer 1

up vote 1 down vote accepted

@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
    }
}
share|improve this answer
    
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 '11 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 '11 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 '11 at 18:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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