In this tutorial, I am going to show you how you can upload multiple files in any format on the server. And download uploaded files with the selection. This tutorial is only for PHP developers.
Create below files in your root directory.
- index.php
- upload.php
- createzip.php
- az.multi.upload.class.php [Download Link]
For user interface UI I am using Bootstrap 4 you can use your own if you want. In this tutorial you are going to learn two things.
- How to upload multiple files on a server?
- How to download multiple files from the server in ZIP format?
To upload multiple files on a server you can use below class snippet which is written in PHP. With this class, you can upload multiple images file with compression and other files as well. Below class will return the array of uploaded files.
Before you start first you need to create a files table for that run below query.
CREATE TABLE `test`.`files` (
`id` INT NOT NULL AUTO_INCREMENT ,
`filename` TEXT NOT NULL ,
`dt` TIMESTAMP NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
Paste below code in the index.php file which is used to create a form to upload files and table structure that is used to view uploaded files with a checkbox.
<div class="card">
<div class="card-header"><i class="fa fa-fw fa-download"></i> <strong>Download In Zip</strong></div>
<div class="card-body">
<div class="col-sm-4 ml-auto mr-auto border p-3">
<form method="post" enctype="multipart/form-data" action="upload.php">
<div class="form-group">
<label><strong>Upload Files</strong></label>
<div class="custom-file">
<input type="file" name="files[]" multiple class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
<div class="form-group">
<button type="submit" name="upload" value="upload" id="upload" class="btn btn-block btn-dark"><i class="fa fa-fw fa-upload"></i> Upload</button>
</div>
</form>
</div>
<hr>
<div class="table table-responsive">
<form method="post" action="createzip.php">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="bg-primary text-white">
<th width="25">Sr#</th>
<th>File Name</th>
</tr>
</thead>
<tbody>
<?php
$db = new mysqli('localhost','root','','test');
$fileQry = $db->query('SELECT * FROM files');
if($fileQry->num_rows>0){
$s = '';
while($row = $fileQry->fetch_assoc()){
$s++;
?>
<tr>
<td><?php echo $s;?></td>
<td>
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" name="fileId[]" class="custom-control-input" id="checkbox<?php echo $row['id']?>" value="<?php echo $row['id']?>">
<label class="custom-control-label" for="checkbox<?php echo $row['id']?>"><?php echo $row['filename'];?></label>
</div>
</td>
</tr>
<?php
}
} ?>
<tr>
<td colspan="2"><button type="submit" name="createzip" id="createzip" value="createzip" class="btn btn-primary"><i class="fa fa-archive"></i> Download All</button></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
Now create a upload.php file and paste below code in it.
<?php
include_once('az.multi.upload.class.php');
$rename = rand(1000,5000).time();
$upload = new ImageUploadAndResize();
$upload->uploadMultiFiles('files', 'uploads', $rename, 0755);
$db = new mysqli('localhost','root','','test');
foreach($upload->prepareNames as $name){
$sql = "INSERT INTO files (filename) VALUES ('".$name."')";
$flag = 0;
if ($db->query($sql) === TRUE) {
$flag = 1;
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
}
if($flag = 1){
header('location:index.php?msg=ras');
exit;
}
?>
After selecting uploaded files you can download all files in ZIP Format. For that you need to paste below code in a createzip.php file.
<?php
if(isset($_REQUEST['createzip']) and $_REQUEST['createzip']!=""){
extract($_REQUEST);
$filename = 'your-zip-file-name.zip';
$db = new mysqli('localhost','root','','test');
$fileQry = $db->query('SELECT * FROM files WHERE id IN ('.implode(",",$fileId).')');
$zip = new ZipArchive;
if ($zip->open($filename, ZipArchive::CREATE)){
while($row = $fileQry->fetch_assoc()){
$zip->addFile(getcwd().'/'.'uploads/'.$row['filename'], $row['filename']);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$filename");
header("Content-length: " . filesize($filename));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$filename");
unlink($filename);
}else{
echo 'Failed!';
}
}
?>
You also can use below headers to download file forcefully in PHP.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);