Convert fwrite(server) to fwrite(local) with "Save As" dialog
This function creates an .ini file and saves it to a folder on the server. What would I need to do to create a version that gives the user the option to save the file locally?
function wpseT开发者_开发问答est()
{
$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');
}
Well, if you're just trying to let the person download, then then store it locally (call wpseTest()), and redirect them to somewhere with the appropriate headers (this can be done via iframe or a new window/tab):
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');
精彩评论