serverA wrap (bzip2) database recordset, serverB unwrap it

$mysql = new dbase();

$rows_USERS = $mysql->getSet("select * from mytable limit 5000", null);

//encode recordset array to json string
$s = json_encode($rows_USERS);

//compress string with ratio 9
$out = bzcompress($s, 9);

//encode data with MIME base64
$out2 = base64_encode($out);

//write to file
file_put_contents('the_recs.dat', $out2);

///////////////////////////post to serverB
$target_url = 'http://serverB/upload.php';

	//This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('./the_recs.dat');
	/* extra_info will appear at $_POST (Note that you can include other arguments in the post along with the file. This allows you to authenticate the upload)
	 * my_file will appear at $_FILES
$post = array('extra_info' => '123456', 'my_file'=> new CurlFile($file_name_with_full_path, 'application/octet-stream', ''));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;


//read file content               
$out = file_get_contents('the_recs.dat');
//decode data encoded with MIME base64
$out2 = base64_decode($out);
$out3 = bzdecompress($out2);
//decode json string              
$out4 = json_decode($out3, true);           //When TRUE, returned objects will be converted into associative arrays. Otherwise will be stdClass

$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['my_file']['name']);

echo '<pre>';
	if (move_uploaded_file($_FILES['my_file']['tmp_name'], $uploadfile)) {
	    echo "File is valid, and was successfully uploaded.\n";
	} else {
	    echo "Possible file upload attack!\n";
	echo 'Here is some more debugging info:';
	echo "\n<hr />\n";
print "</pre>\n";


pure		= 856kb
gzcompress 	= 119kb
bzcompress 	= 71.7kb
3959 records -bzip2- 1.63mb => 302kb

Encoding the compressed data into json string 
got better results with json vs serialize ( on a long recordset.

ref –

By: |04/11/2017|categories: