Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to create and delete zip folders using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.46k
    Comment on it

    Hello Reader's, If you want to make folder zip functionality through website then PHP give you to do this. PHP uses  ZipArchive() to create the zip files from the destination path provided. So lets get started working creating a zip folder. It's code will go like this:-

        $directory = "send_sms/";
        //create zip object
        $zip = new ZipArchive();
        $zip_name = time().".send_sms";
        $zip->open($zip_name,  ZipArchive::CREATE);
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($directory),
        foreach ($files as $file) {
            $path = $file->getRealPath();
            //check file permission
                $zip->addFromString(basename($path),  file_get_contents($path)) ;
                echo "<span style='color:green;'>{$path} is added to zip file.<br /></span> " ;
                echo"<span style='color:red;'>{$path} location could not be added to zip<br /></span>";

    In the code above you can see the $directory is assigned with "send_sms/" path. This is the folder name which is in same destination with name "send_sms". All the various files inside it will be auto zipped to a new folder in this same directory. 
    Now second we will delete folder using PHP. It's code will go like this:-

    $file = "test.txt";
    if (!unlink($file))
    { echo "Folder is deleted successfully";
    }else{ echo "Folder is not deleted";

    In the code above you just have to put file path and name in $fle var. Then unlink function will delete this file. And the output of this is show in the screenshots below

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: