web_master Posted August 17, 2008 Share Posted August 17, 2008 hi, can someone tel me why this script work on my local machine, and dont work on server (internet)... thnxs <?php mysql_connect("localhost", "user", "password"); require("class_mysqldump.php"); $dump = new MySQLDump(); $myString = $dump->dumpDatabase("database"); $content_len = count($myString); $filename = date("Ymd")."_npn_dBase"; $output_file = $filename . ".sql"; header("Content-Disposition: attachment; filename=" . $output_file); header("Content-Type: text/html"); header('Content-Length: ' . $content_len); //echo $myString; exit(); ?> this is the class <?php class MySQLDump { /** * Dump data and structure from MySQL database * * @param string $database * @return string */ function dumpDatabase($database) { // Set content-type and charset header ('Content-Type: text/html; charset=iso-8859-1'); // Connect to database $db = @mysql_select_db($database); if (!empty($db)) { // Get all table names from database $c = 0; $result = mysql_list_tables($database); for($x = 0; $x < mysql_num_rows($result); $x++) { $table = mysql_tablename($result, $x); if (!empty($table)) { $arr_tables[$c] = mysql_tablename($result, $x); $c++; } } // List tables $dump = ''; for ($y = 0; $y < count($arr_tables); $y++){ // DB Table name $table = $arr_tables[$y]; // Structure Header $structure .= "-- \n"; $structure .= "-- Table structure for table `{$table}` \n"; $structure .= "-- \n\n"; // Dump Structure $structure .= "DROP TABLE IF EXISTS `{$table}`; \n"; $structure .= "CREATE TABLE `{$table}` (\n"; $result = mysql_db_query($database, "SHOW FIELDS FROM `{$table}`"); while($row = mysql_fetch_object($result)) { $structure .= " `{$row->Field}` {$row->Type}"; $structure .= (!empty($row->Default)) ? " DEFAULT '{$row->Default}'" : false; $structure .= ($row->Null != "YES") ? " NOT NULL" : false; $structure .= (!empty($row->Extra)) ? " {$row->Extra}" : false; $structure .= ",\n"; } $structure = ereg_replace(",\n$", "", $structure); // Save all Column Indexes in array unset($index); $result = mysql_db_query($database, "SHOW KEYS FROM `{$table}`"); while($row = mysql_fetch_object($result)) { if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) { $index['PRIMARY'][$row->Key_name] = $row->Column_name; } if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) { $index['UNIQUE'][$row->Key_name] = $row->Column_name; } if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) { $index['INDEX'][$row->Key_name] = $row->Column_name; } if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) { $index['FULLTEXT'][$row->Key_name] = $row->Column_name; } } // Return all Column Indexes of array if (is_array($index)) { foreach ($index as $xy => $columns) { $structure .= ",\n"; $c = 0; foreach ($columns as $column_key => $column_name) { $c++; $structure .= ($xy == "PRIMARY") ? " PRIMARY KEY (`{$column_name}`)" : false; $structure .= ($xy == "UNIQUE") ? " UNIQUE KEY `{$column_key}` (`{$column_name}`)" : false; $structure .= ($xy == "INDEX") ? " KEY `{$column_key}` (`{$column_name}`)" : false; $structure .= ($xy == "FULLTEXT") ? " FULLTEXT `{$column_key}` (`{$column_name}`)" : false; $structure .= ($c < (count($index[$xy]))) ? ",\n" : false; } } } $structure .= "\n);\n\n"; // Header $structure .= "-- \n"; $structure .= "-- Dumping data for table `$table` \n"; $structure .= "-- \n\n"; // Dump data unset($data); $result = mysql_query("SELECT * FROM `$table`"); $num_rows = mysql_num_rows($result); $num_fields = mysql_num_fields($result); for ($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_object($result); $data .= "INSERT INTO `$table` ("; // Field names for ($x = 0; $x < $num_fields; $x++) { $field_name = mysql_field_name($result, $x); $data .= "`{$field_name}`"; $data .= ($x < ($num_fields - 1)) ? ", " : false; } $data .= ") VALUES ("; // Values for ($x = 0; $x < $num_fields; $x++) { $field_name = mysql_field_name($result, $x); $data .= "'" . str_replace('\"', '"', mysql_escape_string($row->$field_name)) . "'"; $data .= ($x < ($num_fields - 1)) ? ", " : false; } $data.= ");\n"; } $data.= "\n"; $dump .= $structure . $data; $dump .= "-- --------------------------------------------------------\n\n"; } return $dump; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/ Share on other sites More sharing options...
DeanWhitehouse Posted August 17, 2008 Share Posted August 17, 2008 I believe this could be something to do with the PHP version on the server. Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/#findComment-618701 Share on other sites More sharing options...
web_master Posted August 17, 2008 Author Share Posted August 17, 2008 well, I can save the file - open the window for save, and save it the file on comp but the size of file is 0 (zero) - nothing is in Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/#findComment-618711 Share on other sites More sharing options...
DeanWhitehouse Posted August 17, 2008 Share Posted August 17, 2008 Of course it will, you cant download .php files direct from the web Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/#findComment-618717 Share on other sites More sharing options...
web_master Posted August 17, 2008 Author Share Posted August 17, 2008 Of course it will, you cant download .php files direct from the web ok, what is the solution? Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/#findComment-618719 Share on other sites More sharing options...
web_master Posted August 20, 2008 Author Share Posted August 20, 2008 can somebody help me what is the solution in this case? Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/#findComment-620849 Share on other sites More sharing options...
PFMaBiSmAd Posted August 20, 2008 Share Posted August 20, 2008 Your code has little to no error checking, no error reporting (it in fact is suppressing errors at one point using an @), no error logging (optional for learning type applications, required for real life applications), and no error recovery logic. Any of your php function calls could be failing and your code blindly continues execution. You need to read the function definitions in the php manual and add appropriate error checking logic to find out at what is working and what is failing. Short answer - your code has no logic in it to tell you why it is not working and we can't tell you exactly why it is failing either. You can add the following two lines immediately after your first opening <?php tag to get any php produced errors to be displayed - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/120101-save-to-file-dont-work/#findComment-621074 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.