PHP Create Zip File And Add Files Example

PHP , Bootstrap , Bootstrap 4

Nicesnippets

2142

17-08-2020


PHP Create Zip File And Add Files Example

Hi Dev,

In this blog,I will explain you how to create zip file of multiple uploaded files using php. we will show example of php multiple files uploaded using create zip file.Zip file is unarguably the popular file format across platforms. We should be well equipped to handle it programmatically. We will see about how to create a zip file of multiple files using PHP for the use-case of multiple file upload.

Creating the zip file consisting of multiple files will be helpful in many use case scenarios. Some of them are listed below.

->While exporting collection of photos as a zip from the gallery or any kind of digital album the extracted data could be downloaded in the form of compressed zip.

->In a job portal the user uploaded qualification, curriculum vitae documents are compressed into a zip format that can be downloadable by admin.

->Online stores selling intellectual products are generally in the form of zip containing the software product files.

Here, I will give you full example for simply create zip file of multiple uploaded files using php as bellow.

Step 1: Create Bootstrap File Upload Interface


In this step,I will used Bootstrap for developing the file upload interface. With the default HTML multiple property a file input field in this interface is used to choose multiple files.

Create Index.php File

<html>

<head>

<title>PHP Create Zip File And Add Files Example - nicesnippets.com</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<body class="bg-info">

<?php include 'upload.php'; ?>

<div class="container">

<div class="row">

<div class="col-md-6 offset-3">

<div class="card mt-5">

<div class="card-header">

<h5>PHP Create Zip File And Add Files Example</h5>

</div>

<div class="card-body">

<?php

if(!empty($error)) {

?>

<p class="alert alert-danger"><?php echo $error; ?></p>

<?php

}

?>

<?php

if(!empty($success)) {

?>

<p class="alert alert-success text-center">

Files uploaded successfully and compressed into a zip format

</p>

<p class="success text-center">

<a href="uploads/<?php echo $success; ?>" class="btn btn-success btn-sm" target="__blank">Click here to download the zip file</a>

</p>

<?php

}

?>

<form action="" method="post" enctype="multipart/form-data">

<div class="input-group">

<div class="input-group-prepend">

<input type="submit" class="btn btn-primary" value="Upload">

</div>

<div class="custom-file">

<input type="file" class="custom-file-input" name="img[]" multiple>

<label class="custom-file-label" >Choose File</label>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

</body>

</html>

Step 2: Create File Upload using zip

In this last step. I will create upload.php file.Once the output zip file is created with the uploaded files, the zip instance is destructed by calling the ZipArchive close method. After that, the created zip file source path is sent as the response.

I will seen how to upload files to a target folder using PHP. The PHP move_uploaded_file($filename, $destination) function is used to do this. It requires the name of the file to be uploaded and the destination path.

Create upload.php File

<?php

if ($_FILES && $_FILES['img']) {

if (!empty($_FILES['img']['name'][0])) {

$zip = new ZipArchive();

$zip_name = getcwd() . "/uploads/upload_" . time() . ".zip";

// Create a zip target

if ($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {

$error .= "Sorry ZIP creation is not working currently.<br/>";

}

$imageCount = count($_FILES['img']['name']);

for($i=0;$i<$imageCount;$i++) {

if ($_FILES['img']['tmp_name'][$i] == '') {

continue;

}

$newname = date('YmdHis', time()) . mt_rand() . '.jpg';

// Moving files to zip.

$zip->addFromString($_FILES['img']['name'][$i], file_get_contents($_FILES['img']['tmp_name'][$i]));

// moving files to the target folder.

move_uploaded_file($_FILES['img']['tmp_name'][$i], './uploads/' . $newname);

}

$zip->close();

// Create HTML Link option to download zip

$success = basename($zip_name);

} else {

$error = '<strong>Error!! </strong> Please select a file.';

}

}

After Create Upload Folder

It will help You..


Recommended Posts