I have code which, through PHP, performs a backup of my database. I've also downloaded code which performs this function off of the internet.
When debugging, it works perfectly. When running through my webbrowser, ( IE, Opera), the file is truncated after a variable number of bytes, between 2700 and 2700 bytes.
Any guesses why this would be happening?
Attached is the code for the class
class backupData{
var $dbserver;
var $dbuser;
var $dbpass;
var $dbname;
function InitVars( $DBServer, $DBUser, $DBPsswd, $DBName){
$this->dbserver = $DBServer;
$this->dbuser = $DBUser;
$this->dbpass = $DBPsswd;
$this->dbname= $DBName;
}
function GetDBStructure(){
mysql_connect($this->dbserver, $this->dbuser, $this->dbpass) or die( "cannot log in");
mysql_select_db($this->dbname) or die( "cannot select your db: --".$this->dbname."--");
$tables = mysql_list_tables($this->dbname);
while ($td = mysql_fetch_array($tables)){
$table = $td[0];
$r = mysql_query("SHOW CREATE TABLE `$table`");
if ($r){
$insert_sql = "";
$d = mysql_fetch_array($r);
$d[1] .= ";";
$SQL[] = str_replace("\n", "", $d[1]);
$table_query = mysql_query("SELECT * FROM `$table`");
$num_fields = mysql_num_fields($table_query);
while ($fetch_row = mysql_fetch_array($table_query)){
$insert_sql .= "INSERT INTO $table VALUES(";
for ($n=1;$n<=$num_fields;$n++){
$m = $n - 1;
$insert_sql .= "\"".$fetch_row[$m]."\", ";
}
$insert_sql = substr($insert_sql,0,-2);
$insert_sql .= ");\n";
}
if ($insert_sql != ""){
$SQL[] = $insert_sql;
}
}
}
return $SQL;
}
function MakeBackup($filename){
$sql = $this->GetDBStructure();
header('Content-type: plain/text');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$sql = implode("\r", $sql);
print $sql;
exit;
}
function restoreBackup ($filename){
mysql_connect($this->dbserver, $this->dbuser, $this->dbpass);
mysql_select_db($this->dbname);
$lines = explode("\n", file_get_contents($filename));
$result = true;
foreach ($lines as $line)
mysql_query($line) OR DIE("Error while importing: ".mysql_error());
//$result = $result && mysql_query($line);
return $result;
}
}//end of class definition
|
And the code to use the class is:
$bkp= new backupData();
$bkp->InitVars( DBHOST, $dbuser, $pswd, "db_name");
$bkp->MakeBackup( "testingonly.sql");
|
Again, it works wonderfully when debugging.
Thanks!