Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to unzip a file in WordPress? WordPress


The plugin linked below adds an admin page, which allows the user to upload a zip file to their site, which <strong>should</strong> unzip itself and dump the contents into the sites uploads folder. However it does not appear to be working. The contents are not dumped into the uploads folder at all.

Any ideas why?

<strong>[[LINK href=""]][[/LINK]]</strong>

Answers (4)


Kailey Lampert answers:

Try this - replace WP_Filesystem(); (line 51) with this:

function __return_direct() { return 'direct'; }
add_filter( 'filesystem_method', '__return_direct' );
remove_filter( 'filesystem_method', '__return_direct' );

I have a plugin called [[LINK href=""]]Upload Media by Zip[[/LINK]], and this seems to work in instances where the zip file can be uploaded, but for some reason not extracted.

Ryan Hellyer comments:

Awesome. That worked perfectly :)

Do you know how exactly that helped?

Kailey Lampert comments:

Seems to be some sort of permissions issue on certain shared hosting setups (I'm afraid I don't know all the details).

This forces WP to use the 'direct' method when working with the filesystem, rather than possible alternatives like FTP.

Ryan Hellyer comments:

Thanks for the snappy reply :) Your help was much appreciated.


Albert Shala answers:

Check your file permissions, set them to something higher like 775 to test it out and see if it works. See this for more info:

Ryan Hellyer comments:

Folder/file permissions are set correctly.


Peter Michael answers:

Use the PHP function to do the unzip, see [[LINK href=""]][[/LINK]] for a quick code snippet.

Ryan Hellyer comments:

You should suggest people use the WordPress API which includes an unzipping tool, not access raw PHP functions directly like that.


Dylan Kuhn answers:

It works for me, on the plugin zip file itself. Maybe see if you're getting an error from the unzip_file() call:

// Unzip to uploads folder
$unzip_result = unzip_file( $data['tmp_name'], $folder['basedir'] );
var_dump( $unzip_result );

// Display link to uploads folder
?><p><strong><a href="<?php echo $folder['baseurl']; ?>">Link to uploads folder</a></strong></p><?php

Ryan Hellyer comments:

Interesting that it worked fine for you.

Kailey Lampert thankfully knew how to solve my problem though.