Location: PHPKode > projects > SQLite PHP Admin > spa/export.php
<?php
/***********************************************************************

		Copyright (c) 2011, Andrew V. Pleshakov	(hide@address.com)
		All rights reserved.

		This file is part of SPA (SQLite PHP Admin)
		See file LICENSE.txt for details

************************************************************************/

require_once('include.php');
if(!$pla_db) {
	header('Location: main.php');
	die;
}
if(!isset($_REQUEST['object'])) {
	$tbls = $pla_db->getTables();
	$tar['tbs'] = '';
	foreach($tbls as $row) {
		$tar['dbb'] = basename($current_db);
		$tar['tbs'] .= "<tr>\n\t<th>".strtoupper($row)."</th>\n";
		$tar['tbs'] .= "\t<td align=\"center\"><input type=\"checkbox\" name=\"str_{$row}\" checked=\"checked\" /></td>\n";
		$tar['tbs'] .= "\t<td align=\"center\"><input type=\"checkbox\" name=\"dat_{$row}\" checked=\"checked\" /></td>\n";
		$tar['tbs'] .= "</tr>\n\n";
	}
	die(design_render('db/export',$tar));
}

$object = $_REQUEST['object'];
$tar['generated_by'] = "-- SQLite PHP Admin Export
-- Generated on " .date("D, j M Y H:i:s T"). "
-- SQLite Version " .$pla_db->libVersion(). "
-- PHP Version " .phpversion(). "\n\n";

if($object == 'pla_full_db_export') {
// export all db with checked tables
	$infile = isset($_POST['gzon']);
	$datum = '';
	$tbls = $pla_db->getTables();
	foreach($tbls as $row) {
		if(isset($_POST["str_{$row}"])) $datum .= TStructure($row);
		if(isset($_POST["dat_{$row}"])) $datum .= TData($row,$infile);
	}
	unset($tar['table']);
	if($infile) TXTExport(basename($current_db).'.sql',$tar['generated_by'].$datum);
}
else {
	$datum = TStructure($object) . TData($object);
	$tar['table'] = $object;
}

$tar['datum'] = $datum;

die(design_render('table/export',$tar));
/////////////////////////////////////////////////
function TStructure($object) {
	global $pla_db;
	$pla_db->query("SELECT sql FROM sqlite_master WHERE tbl_name='{$object}'");
	if($row = $pla_db->fetchArray()) {
			$schema = "-- Table structure for table '{$object}'\n";
			$schema .= $row[0]. ";\n\n";
	}
	else $schema = '';
	return $schema;
}

function TData($object, $infile = false) {
	global $pla_db;
	$pla_db->query("SELECT * FROM '{$object}'");

	for ($i=0; $i<$pla_db->numFields(); $i++)
		$column[$i] = "'".$pla_db->fieldName($i)."'";
	$columns = implode(", ", $column);

	$queries = '';
	while($row = $pla_db->fetchArray()) {
		foreach($row as $k=>$v) $row[$k] = $pla_db->escapeString($v);
		$values = implode("', '", $row);
		if(!$infile)
			$values = "'" .htmlentities(iconv($encoding,DEF_CHARSET,$values),ENT_QUOTES,DEF_CHARSET). "'";
		else
			$values = "'" .iconv($encoding,DEF_CHARSET,$values). "'";
		$queries .= "INSERT INTO '{$object}' (" .$columns. ") VALUES
	(" .$values. ");\n";
	}
	return "-- SQL data dump for table '$object'\n" . $queries . "\n";
}

function GZExport($name,$datum) {
	header('Content-Type: application/x-gzip');
	header('Content-Disposition: attachment; filename="'.$name.'"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
	header('Pragma: public');
	die(gzencode($datum,7));
}

function TXTExport($name,$datum) {
	header('Content-Type: text/plain');
	header('Content-Disposition: attachment; filename="'.$name.'"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
	header('Pragma: public');
	print $datum;
	die; // in peace
}
Return current item: SQLite PHP Admin