打开文件,写入文件,将文件另存为zip,然后流式传输给用户以进行下载 [英] Open file, write to file, save file as a zip and stream to user for download

查看:152
本文介绍了打开文件,写入文件,将文件另存为zip,然后流式传输给用户以进行下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以给我一些有关创建此脚本的建议,我将在下面的现有插件(请参见下面的代码)中添加该建议.

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.

所以我现在拥有的(带有下面的脚本)是一种将预定义的默认值插入到wordpress网站中的方法.我要添加的是一个帮助程序实用程序,由一个按钮或仅显示复制设置"的链接激活,它将使用站点的现有设置(sb2_options),将其写入文件,然后打包生成的文件,连同原始文件一起转换成新的zip文件,该文件实际上成为原始插件的自定义副本,供在其他站点中使用.

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.

因此,代码需要获取包含静态代码的现有.php文件,将其打开以进行编写,然后从wordpress选项表中插入与特定前缀匹配的所有名称/值对(例如,我所有的自定义选项)前缀为"sb2_").完成此操作后,它将把生成的文件保存为"plugin.zip",例如,并将其流式传输给用户以供下载.

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');

推荐答案

对您来说读写文件并不难,但这是

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

这篇关于打开文件,写入文件,将文件另存为zip,然后流式传输给用户以进行下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆