how to create zip file with php
![]() |
| php create zip file |
when you upload a file it will create a zip file automatically and you will get a download option to download current zip file..
just use the below codes to create zip file with php..
step 1:
create 2 files and 1 folder..
1. index.php
2. style.css
folder name is : files
index.php
<html>
<head>
<title>php create zip file and download</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="content">
<h2>Upload and Create zip file with php</h2>
<form method="POST" enctype="multipart/form-data">
<input type="file" name='file'/>
<input type="submit" name="upload" value="Upload Now"/>
</form>
</body>
</html>
<?php
if(isset($_POST['upload']))
{
$uploadfile=$_FILES["file"]["tmp_name"];
$folder="files/";
$file_name=$_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], "$folder".$_FILES["file"]["name"]);
$zip = new ZipArchive(); // Load zip library
$zip_name =$file_name.'.zip'; // zip name
if($zip->open('files/'.$zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
echo "Sorry ZIP creation failed";
}
//Creating zip file
$zipcreate=$zip->addFile("files/".$file_name);
if($zipcreate==TRUE){
echo 'Your Image Uploaded and Zip File Created..<a href=files/'.$file_name.'.zip><font style=color:red size=4>Download Now</font></a>';
}else{
echo 'Failed';
}
$zip->close();
}
?>
</div>
style.css
input[type=file]{
padding:5px 0px 5px 5px;
background:black;
border-radius:5px;
}
input[type=submit]{
background:black;
color:white;
padding:6px 8px;
font-weight:bold;
cursor:pointer;
border-radius:5px;
}
#content{
width:800px;
background:#99ccff;
margin:auto;
overflow:hidden;
}
That's it Friends how to create zip file with php..Thank you for visiting ..if you like this post please share with your friends..



No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.