Open file, write to file, save file as a zip and stream to user for download

I'd like if someone could give me some advice on creating this script, which I will add to the existing plug-in (see code below) script below.

So what I have now (with the script below) is a means to insert a predefined set of defaults into the wordpress site. What I'm wanting to add, is a helper utility, activated by a button or link that just reads "Copy Settings", that will take a site's existing settings (the sb2_options), write that to a file, then package the resulting file, along with the original file into a new zip file that essentially becomes a custom copy of the original plug-in for use in another site.

So the code needs to take an existing .php file containing the static code, open it up for writing, then insert all the name/value pairs from the wordpress options table matching a specific prefix (for example, all my custom options are prefixed with "sb2_"). Once it's done this, it would save the resulting file as "plugin.zip", for example and stream it to the user for download.

Here is the code that I have now, which sets up the site's defaults...

<?php
/**
 * Plugin Name: my plugin
 * Description: Sets up your sites defaults.
 * Version: 1.0
 */

function sb2_plugin_init() 
{

    if ( get_option( 'sb2_plugin' ) == "") 
    {

        //Begin Insert List here. Open the file and write out all the name value pairs, just like in the example.

        //Option 1",
        $sb2_option1 = "test";

        //Option 2",
        $sb2_option2 = "test";


        //Option 1",
        $sb2_option3 = "test";

        //End insert list here

        //update site defaults
        update_option('sb2_option1', sb2_option1);
        update_option('sb2_option2', sb2_option2);
        update_option('sb2_option3', sb2_option3);
        //etc


        // Create post objects
        $my_post = array();
        $my_post['post_title'] = 'Main Blog Post Title';
        $my_post['post_content'] = 'Main Blog Post Content';
        $my_post['post_type'] = 'post';

        //TODO >>> NEED TO MAKE THE POST STICKY

        // Insert the post into the database
        wp_insert_post($my_post);

        wp_cache_flush();
        update_option('sb2_plugin', "1");
    }
}

add_action( 'init','sb2_plugin_init');

Solutions

Reading and writing to file shouldn't be difficult for you but here is a good way to create zip files .

Similar questions

File exists but PHP throwing "failed to open stream: no such file or directory"
I am trying to pull a random string from a .txt file on my server in order to display it in the footer of my site. Here is the code I am using (the first two lines are purely for debugging purposes): And here is what gets displayed in my footer: For some reason, even through the file is being recognised when being included and the directory is corr...
Links don't open new page in Safari Iphone browser unless you hold and select Open/Open in New Page
I am working with a Wordpress site, and recently the links have become deprecated in the mobile Safari browser. None of the links will allow you to click through with the usual touch gestures, though the links animate as if they have been clicked. The user now must hold down the link until Safari's menu pops up and select 'open'. My question is wha...
Use php to download latest wordpress zip file?
I am working on a script to creates a database and installs WordPress. I have the database created but I am not sure how to download WordPress and put it in the correct directory. I also tried wget with exec but that didn't work either.
How to enable .zip file download in a directory inside wordpress website
I have created a wordpress website. I have created a directory named my-resources in the root directory & uploaded few zip files to it via FTP. Now I am trying to download the zip files by directly entering the zip file URL in a browser but it is always showing the wordpress default 404 page. I tried to enable using .htaccess without any succes...
Why can't I force users to login to download a zip file from my website?
I have a directory which contains a zip file and a pptx (PowerPoint) file. In the directory is this .htaccess (regex?) code: When a user puts in the direct URL for pptx file, they are correctly served a 404 unless they are logged in. For zip and 7z (7zip) files they can still download those files without being logged in, even though those file exte...
Setting up a gallery with images and a zip download
I am putting together a little gallery/portfolio for a friend's work and I am struggling to work out where a plugin starts and theme begins. I'm trying to achieve a simple grid of images which link to a detailed view. Inside the detailed view it is just the image, a little description and then a zip download button. How can I do this with WordPress...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .