Skip to main content

Simple Online File Archiver with Comment


In this tutorial, we will create a Simple Online File Archiver with Comment using PHP. This code can compressed multiple files at the same time and write comments on the archive to mark who is the owner of those files.

First thing to do

You have to download and install XAMPP or any local server that run PHP scripts. In my case I used XAMPP as my local server, here's the download link for XAMPP server . If you already installed a local server (XAMPP, WAMP, etc.), just ignore this step.

The Snippet Function

This code will try to compress all the files that you upload then create a zip file. Once the zip file is created, the script downloads the archive then the uploaded file(s) and the generated archive is deleted from the server to save space. If you don't get the logic, take a look at the bunch of code below and the comments on it.

  1. <?php
  2. //checks if submit button is pressed
  3. if(ISSET($_POST['submit'])){
  4. //if error is not equal to 0, show message
  5. if(array_sum($_FILES['upload']['error']) > 0){
  6. echo "No Selected File";
  7. }else{
  8. // generate filename based on archive generation DateTime
  9. $zipname = "FILE-".date('njyHis').".zip";
  11. $archive = new ZipArchive();
  12. $archive->open($zipname, ZipArchive::CREATE);
  13. $files = $_FILES['upload'];
  15. //Add all uploaded files into the archive
  16. for($i = 0; $i < count($files['name']); $i++){
  17. $tmp_name = $files['tmp_name'][$i];
  18. $filename = $files['name'][$i];
  20. move_uploaded_file($tmp_name, $filename);
  22. $archive->addFile("$filename");
  23. }
  24. // write comment to archive before closing
  25. $archive->setArchiveComment(file_get_contents("comment.txt"));
  27. //closes the archive. This means you cannot edit the archive.
  28. $archive->close();
  30. //code to download the generated archive
  31. header('Content-type: application/zip');
  32. header('Content-Disposition: attachment; filename="'.$zipname.'"');
  33. readfile($zipname);
  35. //deletes all uploaded files
  36. foreach($files['name'] as $filedel){
  37. unlink($filedel);
  38. }
  40. //deletes the generated archive
  41. unlink($zipname);
  43. //show success message
  44. echo "Successfully compressed the file";
  45. }
  46. }
  47. ?>

Tutorial done. I hope that this simple tutorial help you. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.