How to create zip file after upload file using PHP

Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to create zip files. In this article we will see how to create zip file after upload file in PHP. You may also like How to create new folder using php and How to check a folder doesn’t already exist using php


        <form method="post" action="" enctype="multipart/form-data">
            <input type="file" name='file'>
            <input type="submit" name="upload_file">

PHP Code

    $uploadfile = $_FILES["file"]["tmp_name"];
    $folder = "images/";
    $file_name = $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);

    $zip = new ZipArchive(); // Load zip library 
    $zip_name = ""; // Zip name
    if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
        echo "Sorry ZIP creation failed at this time";
    $zip->addFile("images/" . $file_name);

Leave a Reply

Your email address will not be published. Required fields are marked *