How to Upload and Unpack a Zip File using PHP

When you’re emailing or uploading larger files, it always makes sense to compress them first. This decreases file size and also helps avoid file corruption during transfer. The most common compressed file you will encounter is the zip file, since most operating systems come with a basic utility to quickly zip and unzip files. Creating a small function to upload a zip file to your server and unpack in the process isn’t as complicated as you might think.

First let’s create a simple form that will allow us to browse our computer and upload a zip file.


Now comes the uploader function:

open($target_path);
		if ($x === true) {
			$zip->extractTo("/home/var/yoursite/httpdocs/"); // change this to the correct site path
			$zip->close();
			unlink($target_path);
		}
		$message = "Your .zip file was uploaded and unpacked.";
	} else {
		$message = "There was a problem with the upload. Please try again.";
	}
}
?>

To make sure the success and error messages appear, we will also have to add to short pieces of PHP code:

$message

"; ?>

Putting it all together into a PHP file would look like this:

open($target_path);
		if ($x === true) {
			$zip->extractTo("/home/var/yoursite/httpdocs/"); // change this to the correct site path
			$zip->close();
			unlink($target_path);
		}
		$message = "Your .zip file was uploaded and unpacked.";
	} else {
		$message = "There was a problem with the upload. Please try again.";
	}
}
?>




Untitled Document


$message

"; ?>

Zip File Icon: Ritesh Ranjan

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie