Jump to content

PHP Save to file, not send to Headers


kool_samule

Recommended Posts

Hi Chaps,

 

I'm working on a PHP script to mysqldump database to file.

 

Using this as a base: http://www.edmondscommerce.co.uk/mysql/php-mysql-dump-script/, I've managed to strip it down to this:

set_time_limit(0);

$mysql_host='localhost';
$mysql_database='database';
$mysql_username='username';
$mysql_password='pasword';

_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
$print_form = 0;

header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$mysql_database."_".date('Y-m-d').'.sql"');

$filename = $mysql_database."_".date('Y-m-d').'.sql';
_mysqldump($mysql_database);

function _mysqldump($mysql_database)
{
$sql="show tables;";
$result= mysql_query($sql);
if( $result)
{
	while( $row= mysql_fetch_row($result))
	{
		_mysqldump_table_structure($row[0]);
			_mysqldump_table_data($row[0]);
	}
}
else
{
	echo "/* no tables in $mysql_database */\n";
}
mysql_free_result($result);
}
function _mysqldump_table_structure($table)
{
echo "/* Table structure for table `$table` */\n";
	echo "DROP TABLE IF EXISTS `$table`;\n\n";
	$sql="show create table `$table`; ";
	$result=mysql_query($sql);
	if( $result)
	{
		if($row= mysql_fetch_assoc($result))
		{
			echo $row['Create Table'].";\n\n";
		}
	}
	mysql_free_result($result);
}
function _mysqldump_table_data($table)
{
$sql="select * from `$table`;";
$result=mysql_query($sql);
if( $result)
{
	$num_rows= mysql_num_rows($result);
	$num_fields= mysql_num_fields($result);
	if( $num_rows> 0)
	{
		echo "/* dumping data for table `$table` */\n";
		$field_type=array();
		$i=0;
		while( $i <$num_fields)
		{
			$meta= mysql_fetch_field($result, $i);
			array_push($field_type, $meta->type);
			$i++;
		}
		echo "insert into `$table` values\n";
		$index=0;
		while( $row= mysql_fetch_row($result))
		{
			echo "(";
			for( $i=0; $i <$num_fields; $i++)
			{
				if( is_null( $row[$i]))
					echo "null";
				else
				{
					switch( $field_type[$i])
					{
						case 'int':
							echo $row[$i];
							break;
						case 'string':
						case 'blob' :
						default:
							echo "'".mysql_real_escape_string($row[$i])."'";
					}
				}
				if( $i <$num_fields-1)
					echo ",";
			}
			echo ")";
			if( $index <$num_rows-1)
				echo ",";
			else
				echo ";";
			echo "\n";
			$index++;
		}
	}
}
mysql_free_result($result);
echo "\n";
}
function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
{
global $output_messages;
$link = mysql_connect($mysql_host, $mysql_username, $mysql_password);
if (!$link)
{
   array_push($output_messages, 'Could not connect: ' . mysql_error());
}
else
{
	array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");
	$db_selected = mysql_select_db($mysql_database, $link);
	if (!$db_selected)
	{
		array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error());
	}
	else
		array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully");
}
}

 

The problem is, I want the file to save to a specific network location, rather than the output being sent to headers.

 

I've tried to play around with:

ob_start();....
$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.html","w");
fwrite($fp,$page);
fclose($fp);

 

But this outputs to the browser, then saves the file...I would like to load the page, and have the script create and save the file where I want it (without the output in the browser, or prompt to save/download file).

 

Note: I need something like this as I'm running PHP/MySQL on IIS, so can use shell/system/etc.

 

Any help will be hi-5-tastic!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.