Simple Online File Archiver with Comment

Submitted by: 
Language: 
Visitors have accessed this post 1721 times.

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 https://www.apachefriends.org/download.html . 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";
  10.  
  11. $archive = new ZipArchive();
  12. $archive->open($zipname, ZipArchive::CREATE);
  13. $files = $_FILES['upload'];
  14.  
  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];
  19.  
  20. move_uploaded_file($tmp_name, $filename);
  21.  
  22. $archive->addFile("$filename");
  23. }
  24. // write comment to archive before closing
  25. $archive->setArchiveComment(file_get_contents("comment.txt"));
  26.  
  27. //closes the archive. This means you cannot edit the archive.
  28. $archive->close();
  29.  
  30. //code to download the generated archive
  31. header('Content-type: application/zip');
  32. header('Content-Disposition: attachment; filename="'.$zipname.'"');
  33. readfile($zipname);
  34.  
  35. //deletes all uploaded files
  36. foreach($files['name'] as $filedel){
  37. unlink($filedel);
  38. }
  39.  
  40. //deletes the generated archive
  41. unlink($zipname);
  42.  
  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 SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.