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.


$mytest = somefunction();

function somefunction() {
  global $wp_filesystem;


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.


function openZip($file_to_open) { 
    global $target;  
    global $wp_filesystem;
        $zip = new ZipArchive();  
        $x = $zip->open($file_to_open);  
        if($x === true) 
        } else {  
            die("There was a problem. Please try again!");  
    $my_dirs = ''; //What should this be? I'm already passing he $target directory
    _unzip_file_pclzip($file_to_open, $target, $my_dirs);

$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.

| 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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