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
https://forums.phpfreaks.com/topic/261717-php-save-to-file-not-send-to-headers/
Share on other sites

For starters you'll need to remove the two header() calls to stop it from prompting to save the file in the browser.

 

From there you will need to modify the functions to return data rather than echo it.

 

Finally, you can use file_put_contents to save it to a file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.