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 need to obtain a reference to the $wp_filesystem object. In the test below, the var_dump($wp_filesystem) returns NULL. What additional files are required in order to properly set up $wp_filesystem?

I was expecting that since its called in file.php, loading that file would be sufficient to load the object.

<?php
require('../../../wp-blog-header.php');
require('../../../wp-admin/includes/file.php');

$mytest = somefunction();

function somefunction() {
  global $wp_filesystem;
  var_dump($wp_filesystem);

  return;
}
?>

UPDATE: I've found that I can call WP_Filesystem() directly to create it, so I'm getting the zip extracted fine, now the problem is that the zip file is copied to the destination folder rather than deleted as my ZipArchive method does.

require('../../../wp-blog-header.php');
require('../../../wp-admin/includes/file.php');

function openZip($file_to_open) { 
    global $target;  
    global $wp_filesystem;
    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
    {
    WP_Filesystem();
    $my_dirs = ''; //What should this be? I'm already passing he $target directory
    _unzip_file_pclzip($file_to_open, $target, $my_dirs);
    }
} 
share|improve this question
    
xref.yoast.com/2.9.2/_functions/wp_filesystem.html might help. –  Zack Feb 1 '11 at 20:10

1 Answer 1

up vote 2 down vote accepted

$wp_filesystem is a global variable containing the instance of the (auto-)configured filesystem object after the filesystem "factory" has been run.

To run the factory "over" the global variable (so to set it), just call the WP_Filesystem() function which is, guess what, undocumented in codex. At least the docblock contains some information and you can read the sourcecode (if that's an option for you).

Anyway, I would give it a try to add a function call to your code after requiring the file.php from the /wp-admin/includes directory. Probably this already solves your issue.

If you are looking for a (better) file-system abstraction than/next to the built-in one, consider the file-system objects in SPL which is part of PHP already.

share|improve this answer
    
thanks, I had updated my question yesterday to include the WP_Filesystem() call. Now the file is unzipped, however, the original is not deleted from the destination folder, so you get "archive" and "archive.zip" (as an example if your zip is named archive.zip) –  Scott B Feb 2 '11 at 14:20
    
@Scott B - Well, see your code: The code to delete the zip file is not part of the else claues. The overall solution might be that you move it after the if/else block, so to delete it in every case :) –  hakre Feb 2 '11 at 20:42
    
so if I call unlink($file_to_open) outside my else, that should do it? (assuming unlink exists in the pclzip class) –  Scott B Feb 2 '11 at 21:53
    
@Scott B - Yeah, but that made me aware that in case it's not delete-able by that, you might want to prefer the unlink mehtod of the filesystem object: delete(). See core.trac.wordpress.org/browser/trunk/wp-admin/includes/… // $wp_filesystem->delete($file_to_open); –  hakre Feb 3 '11 at 11:27

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.