How to create zip file after upload file using PHP
Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to create zip files. In this article we will see how to create zip file after upload file in PHP.
HTML Code
<html>
<body>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name='file'>
<input type="submit" name="upload_file">
</form>
</body>
</html>
PHP Code
<?php
if(isset($_POST['upload_file']))
{
$uploadfile = $_FILES["file"]["tmp_name"];
$folder = "images/";
$file_name = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);
$zip = new ZipArchive(); // Load zip library
$zip_name = "upload.zip"; // Zip name
if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
echo "Sorry ZIP creation failed at this time";
}
$zip->addFile("images/" . $file_name);
$zip->close();
}
?>
Leave a Reply