32,000+ SUBSCRIBERS CAN'T GO WRONG !!!

phpans
GET TOP ARTICLES, SNIPPETS, SCRIPTS IN YOUR INBOX FOR FREE!

Delivered by GET EMAIL UPDATES FeedBurner
We always respect your privacy and take protecting it seriously.
We won't Spam You

Upload Zip File And Extract In PHP

, 0,
Upload Zip File And Extract In PHP
In this article, we are going to discuss how to Upload Zip File And Extract In PHP. In our last article, we knew Easy Way To Create ZIP File Using PHP and Extract Zip File Using PHP. After that, some guys ask me about zip file extract online after uploading. This article for easy to understand when you upload a zip file that it will be automatically extracted in a server. We already know how to upload a file in PHP and this time, we learn our uploaded file will extract in a server. We use Unzip() function from Extract Zip File Using PHP and for our this project we need to modify and add some extra parameters to do that. Now we are going to create this snippet.

<?php
function Unzip($file,$path)
{
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
}
}
if (isset($_FILES['file'])&&$_FILES['file']['size']>1)
{
$dir = "files";
if (!is_dir($dir))
{
mkdir($dir, 0755);
}
$filename = $_FILES['file']['name'];
$path=$dir.'/'.$filename;
move_uploaded_file($_FILES['file']['tmp_name'], $path);
Unzip($path,$dir);
unlink($path);
echo 'Successfully uploaded and unzipped!';
}
else
{
echo '<form name="form" enctype="multipart/form-data" method="post">';

echo '<input type="file" name="file" accept="application/zip"><br/>';
echo '<input type="submit" value="Upload"/>';
echo '</form>';

}

?>

Thi is our snippet if you run this script then you will see a upload form and there you can upload Zip file and this file will extract after complete uploading. Here we passed two parameter in Unzip() function. a for the file which will be extracted after uploading another one is for extract destination.

we used another function for creating upload directory. If upload directory already exists then this script won't create or rewrite upload directory. If upload directory will not exist then when a file will process for upload this snippet will create upload directory automatically.
<?php
$dir = "files";
if (!is_dir($dir))
{
mkdir($dir, 0755);
}
?>

We used unlink() function in this script. For using this our uploaded file will delete If you want you can remove unlink($path); line from the snippet. That's all If you want to use this snippet you can. I hope you understand all process. If you have any question or comment about Upload Zip File And Extract In PHP article please don't hesitate to do that, please comment your question in comment section.

Download PDF
Is this article helpful for you?





captcha

  • Connect
  •       
Top
Create an free account and start learning & sharing! Sign Up