使用“另存为”对话框将fwrite(服务器)转换为fwrite(local)

时间:2011-07-14 02:11:46

标签: php fwrite

此函数创建.ini文件并将其保存到服务器上的文件夹中。我需要做些什么来创建一个版本,让用户可以选择在本地保存文件?

function wpseTest()
{
    $query = "SELECT option_name, option_value FROM wp_options where option_name like 'test|_%' escape '|' AND option_value > ''";
    global $wpdb;
    $matches = $wpdb->get_results($query);

    $mySettings = "[settings]\r\n";

    foreach ($matches as $result){
        $mySettings .= $result->option_name;
        $mySettings .= ' = ';
        $mySettings .= '"'.str_replace("\r\n", "", addslashes($result->option_value)).'"';
        $mySettings .= "\r\n";
    }
    $mySettingsFileLocation = WP_PLUGIN_DIR.'/test/test-backup.ini';
    $mySettingsFile = fopen($mySettingsFileLocation, 'w');
    fwrite($mySettingsFile, $mySettings);fclose($mySettingsFile);
    copy($mySettingsFileLocation, WP_PLUGIN_DIR.'/test/test-'.date("F-j-Y-g-i-s-a").'.ini');
}

1 个答案:

答案 0 :(得分:0)

好吧,如果您只是想让这个人下载,然后将其存储在本地(调用wpseTest()),并将它们重定向到具有相应标头的某个地方(这可以通过iframe或新窗口完成/标签):

header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"backup.ini\"");
header("Content-Length: ".filesize(WP_PLUGIN_DIR.'/test/test-backup.ini'));
readfile(WP_PLUGIN_DIR.'/test/test-backup.ini');