I am creating a file to extract zip files . The code snipped checks if the file extension is .zip and then if the file already exists. If not, extract the archive.
if($fileExtension == "zip")
{
if(is_dir("unzips/".$fileName) == false)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
$zip = new ZipArchive();
$zip -> open("temp/".$_FILES["file"]["name"]);
for($num = 0; $num < $zip->numFiles; $num++)
{
$fileInfo = $zip->statIndex($num);
echo "Successfully Extracted: ".$fileInfo["name"];
$zip->extractTo("unzips/".$fileName);
echo "<br />";
}
zip_close($zip);
unlink("temp/".$_FILES["file"]["name"]);
}
else
{
echo $fileName." The ZIP archive file has already been unzipped";
}
}
else
{
echo "Only .zip files are allowed";
}
Here's the full code for index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unzipping Zip File</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><input type="submit" name="submit" value="Extract">
</form>
<?php
if(isset($_POST['submit']))
{
$array = explode(".",$_FILES["file"]["name"]);
$fileName = $array[0];
$fileExtension = strtolower(end($array));
if($fileExtension == "zip")
{
if(is_dir("unzips/".$fileName) == false)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
$zip = new ZipArchive();
$zip -> open("temp/".$_FILES["file"]["name"]);
for($num = 0; $num < $zip->numFiles; $num++)
{
$fileInfo = $zip->statIndex($num);
echo "Successfully Extracted: ".$fileInfo["name"];
$zip->extractTo("unzips/".$fileName);
echo "<br />";
}
$zip -> close();
unlink("temp/".$_FILES["file"]["name"]);
}
else
{
echo $fileName." The ZIP archive file has already been unzipped";
}
}
else
{
echo "Only .zip files are allowed";
}
}
?>
<br />
<p id="end note">Please return to main menu and download individually extracted files.</p>
</body>
</html>
directory structure looks like: D:/MyWebs/extracting/index.php D:/MyWebs/extracting/unzips (final destination that contains unzipped folder) D:/MyWebs/extracting/temp (stores zip and then deletes them)