Greystoke Posted September 2, 2009 Share Posted September 2, 2009 Hi, Some the sql database is not getting added to the file when using fputs Code been used: <?php if (!defined('ADMIN_FILE')) { die ("Access Denied"); } global $prefix, $db, $admin_file; $aid = substr("$aid", 0,25); $row = $db->sql_fetchrow($db->sql_query("SELECT radminsuper FROM " . $prefix . "_authors WHERE aid='$aid'")); if ($row['radminsuper'] == 1) { $DATESTRING = 'd-M-Y_His'; $DATESTRING1 = 'l, d F Y @ H:i:s T'; $database = $dbname; $sql_filename = $database.'_'.date($DATESTRING).'.sql'; $backups_dir = '../SQL_Backups/'; if (!function_exists('file_put_contents')) { function file_put_contents($filename, $content, $flags = null, $resource_context = null) { // If $content is an array, convert it to a string if (is_array($content)) { $content = implode('', $content); } if (!is_scalar($content)) { user_error('file_put_contents() The 2nd parameter should be either a string or an array', E_USER_WARNING); return false; } $length = strlen($content); $mode = ($flags & FILE_APPEND) ? 'a' : 'w'; $use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ? true : false; if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) { user_error('file_put_contents() failed to open stream: Permission denied', E_USER_WARNING); return false; } $bytes = 0; if (($bytes = @fwrite($fh, $content)) === false) { $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s', $length, $filename); user_error($errormsg, E_USER_WARNING); return false; } @fclose($fh); if ($bytes != $length) { $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.', $bytes, $length); user_error($errormsg, E_USER_WARNING); return false; } return $bytes; } } if (!function_exists('file_get_contents')) { function file_get_contents($filename, $incpath = false, $resource_context = null) { if (false === $fh = fopen($filename, 'rb', $incpath)) { user_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING); return false; } clearstatcache(); if ($fsize = @filesize($filename)) { $data = fread($fh, $fsize); } else { $data = ''; while (!feof($fh)) { $data .= fread($fh, 8192); } } fclose($fh); return $data; } } switch($op) { case "backup": @set_time_limit(600); $crlf="\n"; switch($lang) { case french : // French Text $strNoTablesFound = "Aucune table n'a été trouvée dans cette base."; $strHost = "Serveur"; $strDatabase = "Base de données"; $strTableStructure = "Structure de la table"; $strDumpingData = "Contenu de la table"; $strError = "Erreur"; $strSQLQuery = "requête SQL"; $strMySQLSaid = "MySQL a répondu:"; $strBack = "Retour"; $strFileName = "Sauvegarde BD"; $strName = "Sauvegarde de la base de données"; $strDone = "effectuée le"; $strat = "à"; $strby = "par"; $date_jour = date ("d-m-Y"); break; default : // English Text $strNoTablesFound = "No tables found in database."; $strHost = "Host"; $strDatabase = "Database "; $strTableStructure = "Table structure for table"; $strDumpingData = "Dumping data for table"; $strError = "Error"; $strSQLQuery = "SQL-query"; $strMySQLSaid = "MySQL said: "; $strBack = "Back"; $strFileName = "Save Database"; $strName = "Database saved"; $strDone = "On"; $strat = "at"; $strby = "by"; $date_jour = date ("m-d-Y"); break; } // header("Content-disposition: filename=$strFileName $dbname $date_jour.sql"); // header("Content-type: application/octetstream"); // header("Pragma: no-cache"); // header("Expires: 0"); // doing some DOS-CRLF magic... $client = $_SERVER["HTTP_USER_AGENT"]; if(ereg('[^(]*\((.*)\)[^)]*',$client,$regs)) { $os = $regs[1]; // this looks better under WinX if (eregi("Win",$os)) $crlf="\r\n"; } function my_handler($sql_insert) { global $crlf; echo "$sql_insert;$crlf"; } // Get the content of $table as a series of INSERT statements. // After every row, a custom callback function $handler gets called. // $handler must accept one parameter ($sql_insert); function get_table_content($db, $table, $handler) { $result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die(); $i = 0; while($row = mysql_fetch_row($result)) { // set_time_limit(60); // HaRa $table_list = "("; for($j=0; $j<mysql_num_fields($result);$j++) $table_list .= mysql_field_name($result,$j).", "; $table_list = substr($table_list,0,-2); $table_list .= ")"; if(isset($GLOBALS["showcolumns"])) $schema_insert = "INSERT INTO $table $table_list VALUES ("; else $schema_insert = "INSERT INTO $table VALUES ("; for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= " NULL,"; elseif($row[$j] != "") $schema_insert .= " '".addslashes($row[$j])."',"; else $schema_insert .= " '',"; } $schema_insert = ereg_replace(",$", "", $schema_insert); $schema_insert .= ")"; $handler(trim($schema_insert)); $i++; } return (true); } // Return $table's CREATE definition // Returns a string containing the CREATE statement on success function get_table_def($db, $table, $crlf) { $schema_create = ""; //$schema_create .= "DROP TABLE IF EXISTS $table;$crlf"; $schema_create .= "CREATE TABLE $table ($crlf"; $result = mysql_db_query($db, "SHOW FIELDS FROM $table") or mysql_die(); while($row = mysql_fetch_array($result)) { $schema_create .= " $row[Field] $row[Type]"; if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0")) $schema_create .= " DEFAULT '$row[Default]'"; if($row["Null"] != "YES") $schema_create .= " NOT NULL"; if($row["Extra"] != "") $schema_create .= " $row[Extra]"; $schema_create .= ",$crlf"; } $schema_create = ereg_replace(",".$crlf."$", "", $schema_create); $result = mysql_db_query($db, "SHOW KEYS FROM $table") or mysql_die(); while($row = mysql_fetch_array($result)) { $kname=$row['Key_name']; if(($kname != "PRIMARY") && ($row['Non_unique'] == 0)) $kname="UNIQUE|$kname"; if(!isset($index[$kname])) $index[$kname] = array(); $index[$kname][] = $row['Column_name']; } while(list($x, $columns) = @each($index)) { $schema_create .= ",$crlf"; if($x == "PRIMARY") $schema_create .= " PRIMARY KEY (" . implode($columns, ", ") . ")"; elseif (substr($x,0,6) == "UNIQUE") $schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")"; else $schema_create .= " KEY $x (" . implode($columns, ", ") . ")"; } $schema_create .= "$crlf)"; return (stripslashes($schema_create)); } function mysql_die($error = "") { echo "<b> $strError </b><p>"; if(isset($sql_query) && !empty($sql_query)) { echo "$strSQLQuery: <pre>$sql_query</pre><p>"; } if(empty($error)) echo $strMySQLSaid.mysql_error(); else echo $strMySQLSaid.$error; echo "<br><a href=\"javascript:history.go(-1)\">$strBack</a>"; exit; } global $dbhost, $dbuname, $dbpass, $dbname; mysql_pconnect($dbhost, $dbuname, $dbpass); @mysql_select_db("$dbname") or die ("Unable to select database"); $tables = mysql_list_tables($dbname); $num_tables = @mysql_numrows($tables); if($num_tables == 0) { echo $strNoTablesFound; } else { $fp = fopen($backups_dir . $sql_filename, 'w'); $i = 0; $heure_jour = date ("H:i"); $schema .= "# ========================================================$crlf"; $schema .= "#$crlf"; $schema .= "# $strName : $dbname$crlf"; $schema .= "# $strDone $date_jour $strat $heure_jour $strby $name !$crlf"; $schema .= "#$crlf"; $schema .= "# ========================================================$crlf"; $schema .= "$crlf"; while($i < $num_tables) { $table = mysql_tablename($tables, $i); $schema .= $crlf; $schema .= "# --------------------------------------------------------$crlf"; $schema .= "#$crlf"; $schema .= "# $strTableStructure '$table'$crlf"; $schema .= "#$crlf"; $schema .= $crlf; $schema .= get_table_def($dbname, $table, $crlf).";$crlf$crlf"; $schema .= "#$crlf"; $schema .= "# $strDumpingData '$table'$crlf"; $schema .= "#$crlf"; $schema .= $crlf; $schema .= get_table_content($dbname, $table, "my_handler"); $i++; } } fputs($fp, $schema); fclose($fp); break; } } else { echo "Access Denied"; } ?> Example of output: # ======================================================== # # Database saved : osc # On 09-02-2009 at 23:18 by ! # # ======================================================== # -------------------------------------------------------- # # Table structure for table 'address_book' # CREATE TABLE address_book ( address_book_id int(11) NOT NULL auto_increment, customers_id int(11) NOT NULL, entry_gender char(1) NOT NULL, entry_company varchar(32), entry_firstname varchar(32) NOT NULL, entry_lastname varchar(32) NOT NULL, entry_street_address varchar(64) NOT NULL, entry_suburb varchar(32), entry_postcode varchar(10) NOT NULL, entry_city varchar(32) NOT NULL, entry_state varchar(32), entry_country_id int(11) DEFAULT '0' NOT NULL, entry_zone_id int(11) DEFAULT '0' NOT NULL, PRIMARY KEY (address_book_id), KEY idx_address_book_customers_id (customers_id) ); # # Dumping data for table 'address_book' # 1 # -------------------------------------------------------- # # Table structure for table 'address_format' # CREATE TABLE address_format ( address_format_id int(11) NOT NULL auto_increment, address_format varchar(128) NOT NULL, address_summary varchar(48) NOT NULL, PRIMARY KEY (address_format_id) ); # # Dumping data for table 'address_format' # 1 # -------------------------------------------------------- # # Table structure for table 'administrators' # CREATE TABLE administrators ( id int(11) NOT NULL auto_increment, user_name varchar(32) NOT NULL, user_password varchar(40) NOT NULL, PRIMARY KEY (id) ); # # Dumping data for table 'administrators' # 1 # -------------------------------------------------------- # # Table structure for table 'banners' # CREATE TABLE banners ( banners_id int(11) NOT NULL auto_increment, banners_title varchar(64) NOT NULL, banners_url varchar(255) NOT NULL, banners_image varchar(64) NOT NULL, banners_group varchar(10) NOT NULL, banners_html_text text, expires_impressions int(7) DEFAULT '0', expires_date datetime, date_scheduled datetime, date_added datetime NOT NULL, date_status_change datetime, status int(1) DEFAULT '1' NOT NULL, PRIMARY KEY (banners_id), KEY idx_banners_group (banners_group) ); # # Dumping data for table 'banners' # 1 # -------------------------------------------------------- Its putting 1 in instead of the data. Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/ Share on other sites More sharing options...
bundyxc Posted September 2, 2009 Share Posted September 2, 2009 Your first code is tldr; Please post the relevant code that you're having trouble with outputting the string/integer "1". Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911266 Share on other sites More sharing options...
Greystoke Posted September 2, 2009 Author Share Posted September 2, 2009 $schema .= get_table_content($dbname, $table, "my_handler"); Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911269 Share on other sites More sharing options...
Greystoke Posted September 3, 2009 Author Share Posted September 3, 2009 Can anyone help please Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911623 Share on other sites More sharing options...
ignace Posted September 3, 2009 Share Posted September 3, 2009 function get_table_content($db, $table, $handler) { $result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die(); $i = 0; while($row = mysql_fetch_row($result)) { // set_time_limit(60); // HaRa $table_list = "("; for($j=0; $j<mysql_num_fields($result);$j++) $table_list .= mysql_field_name($result,$j).", "; $table_list = substr($table_list,0,-2); $table_list .= ")"; if(isset($GLOBALS["showcolumns"])) $schema_insert = "INSERT INTO $table $table_list VALUES ("; else $schema_insert = "INSERT INTO $table VALUES ("; for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= " NULL,"; elseif($row[$j] != "") $schema_insert .= " '".addslashes($row[$j])."',"; else $schema_insert .= " '',"; } $schema_insert = ereg_replace(",$", "", $schema_insert); $schema_insert .= ")"; $handler(trim($schema_insert)); $i++; } return $schema_insert; } Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911723 Share on other sites More sharing options...
Greystoke Posted September 3, 2009 Author Share Posted September 3, 2009 Hi, Thank for taking a look. That kind of fixed it. It is only putting one line of data from each table. # ======================================================== # # Database saved : osc # On 09-03-2009 at 17:09 by ! # # ======================================================== # -------------------------------------------------------- # # Table structure for table 'address_book' # CREATE TABLE address_book ( address_book_id int(11) NOT NULL auto_increment, customers_id int(11) NOT NULL, entry_gender char(1) NOT NULL, entry_company varchar(32), entry_firstname varchar(32) NOT NULL, entry_lastname varchar(32) NOT NULL, entry_street_address varchar(64) NOT NULL, entry_suburb varchar(32), entry_postcode varchar(10) NOT NULL, entry_city varchar(32) NOT NULL, entry_state varchar(32), entry_country_id int(11) DEFAULT '0' NOT NULL, entry_zone_id int(11) DEFAULT '0' NOT NULL, PRIMARY KEY (address_book_id), KEY idx_address_book_customers_id (customers_id) ); # # Dumping data for table 'address_book' # INSERT INTO address_book VALUES ( '1', '1', 'm', 'ACME Inc.', 'John', 'Doe', '1 Way Street', '', '12345', 'NeverNever', '', '223', '12') # -------------------------------------------------------- # # Table structure for table 'address_format' # CREATE TABLE address_format ( address_format_id int(11) NOT NULL auto_increment, address_format varchar(128) NOT NULL, address_summary varchar(48) NOT NULL, PRIMARY KEY (address_format_id) ); # # Dumping data for table 'address_format' # INSERT INTO address_format VALUES ( '5', '$firstname $lastname$cr$streets$cr$postcode $city$cr$country', '$city / $country') Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911736 Share on other sites More sharing options...
ignace Posted September 3, 2009 Share Posted September 3, 2009 function get_table_content($db, $table, $handler) { $schema_insert = ''; $result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die(); $i = 0; while($row = mysql_fetch_row($result)) { // set_time_limit(60); // HaRa $table_list = "("; for($j=0; $j<mysql_num_fields($result);$j++) $table_list .= mysql_field_name($result,$j).", "; $table_list = substr($table_list,0,-2); $table_list .= ")"; if(isset($GLOBALS["showcolumns"])) $schema_insert .= "INSERT INTO $table $table_list VALUES ("; else $schema_insert .= "INSERT INTO $table VALUES ("; for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= " NULL,"; elseif($row[$j] != "") $schema_insert .= " '".addslashes($row[$j])."',"; else $schema_insert .= " '',"; } $schema_insert = ereg_replace(",$", "", $schema_insert); $schema_insert .= ");\n"; $handler(trim($schema_insert)); $i++; } return $schema_insert; } Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911743 Share on other sites More sharing options...
Greystoke Posted September 3, 2009 Author Share Posted September 3, 2009 PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 30515201 bytes) line 160 line 160 = echo "$sql_insert;$crlf"; Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911757 Share on other sites More sharing options...
Greystoke Posted September 3, 2009 Author Share Posted September 3, 2009 Can anyone help, please. Quote Link to comment https://forums.phpfreaks.com/topic/172898-fputs-problem-when-trying-to-dump-an-sql-database-to-file/#findComment-911936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.