Skip to content

Commit e4ee0ef

Browse files
committed
#25 added ability to dump export right on server, without need to download
1 parent 8659c78 commit e4ee0ef

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

phpminiadmin.php

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
'chset'=>"utf8",#optional, default charset
2323
);
2424
$IS_COUNT=false; #set to true if you want to see Total records when pagination occurs (SLOWS down all select queries!)
25+
$DUMP_FILE=dirname(__FILE__).'/ppmdump'; #path to file without extension used for server-side exports (timestamp, .sql/.csv/.gz extension added) or imports(.sql)
2526
file_exists($f=dirname(__FILE__) . '/phpminiconfig.php')&&require($f); // Read from config (easier to update)
2627
if (function_exists('date_default_timezone_set')) date_default_timezone_set('UTC');#required by PHP 5.1+
2728

2829
//constants
29-
$VERSION='1.9.160705';
30+
$VERSION='1.9.161116';
3031
$MAX_ROWS_PER_PAGE=50; #max number of rows in select per one page
3132
$D="\r\n"; #default delimiter for export
3233
$BOM=chr(239).chr(187).chr(191);
@@ -788,7 +789,7 @@ function loadsess(){
788789
}
789790

790791
function print_export(){
791-
global $self,$xurl,$DB;
792+
global $self,$xurl,$DB,$DUMP_FILE;
792793
$t=$_REQUEST['rt'];
793794
$l=($t)?"Table $t":"whole DB";
794795
print_header();
@@ -812,16 +813,24 @@ function print_export(){
812813
<br>
813814
<input type="hidden" name="doex" value="1">
814815
<input type="hidden" name="rt" value="<?php eo($t)?>">
815-
<input type="submit" value=" Download "><input type="button" value=" Cancel " onclick="window.location='<?php eo($self.'?'.$xurl.'&db='.$DB['db'])?>'">
816+
<input type="submit" value=" Download ">
817+
<input type="submit" name="srv" value=" Dump on Server ">
818+
<input type="button" value=" Cancel " onclick="window.location='<?php eo($self.'?'.$xurl.'&db='.$DB['db'])?>'">
819+
<p><small>"Dump on Server" exports to file:<br><?php eo(export_fname($DUMP_FILE).'.sql')?></small></p>
816820
</div>
817821
</center>
818822
<?php
819823
print_footer();
820824
exit;
821825
}
822826

827+
function export_fname($f,$ist=false){
828+
$t=$ist?date('Y-m-d-His'):'YYYY-MM-DD-HHMMSS';
829+
return $f.$t;
830+
}
831+
823832
function do_export(){
824-
global $DB,$VERSION,$D,$BOM,$ex_isgz,$dbh;
833+
global $DB,$VERSION,$D,$BOM,$ex_isgz,$ex_issrv,$dbh,$out_message;
825834
$rt=str_replace('`','',$_REQUEST['rt']);
826835
$t=explode(",",$rt);
827836
$th=array_flip($t);
@@ -836,9 +845,10 @@ function do_export(){
836845
if ($ex_isgz) {
837846
$aext='.gz';$ctp='application/x-gzip';
838847
}
839-
ex_start();
848+
$ex_issrv=($_REQUEST['srv'])?1:0;
840849

841850
if ($ct==1&&$_REQUEST['et']=='csv'){
851+
ex_start('.csv');
842852
ex_hdr($ctp?$ctp:'text/csv',"$t[0].csv$aext");
843853
if ($DB['chset']=='utf8') ex_w($BOM);
844854

@@ -850,29 +860,29 @@ function do_export(){
850860
}
851861
ex_w($D);
852862
while($row=mysqli_fetch_row($sth)) ex_w(to_csv_row($row));
853-
ex_end();
854-
exit;
855-
}
856-
857-
ex_hdr($ctp?$ctp:'text/plain',"$DB[db]".(($ct==1&&$t[0])?".$t[0]":(($ct>1)?'.'.$ct.'tables':'')).".sql$aext");
858-
ex_w("-- phpMiniAdmin dump $VERSION$D-- Datetime: ".date('Y-m-d H:i:s')."$D-- Host: $DB[host]$D-- Database: $DB[db]$D$D");
859-
ex_w("/*!40030 SET NAMES $DB[chset] */;$D");
860-
$ex_super && ex_w("/*!40030 SET GLOBAL max_allowed_packet=16777216 */;$D$D");
861-
ex_w("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;$D$D");
863+
}else{
864+
ex_start('.sql');
865+
ex_hdr($ctp?$ctp:'text/plain',"$DB[db]".(($ct==1&&$t[0])?".$t[0]":(($ct>1)?'.'.$ct.'tables':'')).".sql$aext");
866+
ex_w("-- phpMiniAdmin dump $VERSION$D-- Datetime: ".date('Y-m-d H:i:s')."$D-- Host: $DB[host]$D-- Database: $DB[db]$D$D");
867+
ex_w("/*!40030 SET NAMES $DB[chset] */;$D");
868+
$ex_super && ex_w("/*!40030 SET GLOBAL max_allowed_packet=16777216 */;$D$D");
869+
ex_w("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;$D$D");
870+
871+
$sth=db_query("show tables from `$DB[db]`");
872+
while($row=mysqli_fetch_row($sth)){
873+
if (!$rt||array_key_exists($row[0],$th)) do_export_table($row[0],1,$MAXI);
874+
}
862875

863-
$sth=db_query("show tables from `$DB[db]`");
864-
while($row=mysqli_fetch_row($sth)){
865-
if (!$rt||array_key_exists($row[0],$th)) do_export_table($row[0],1,$MAXI);
876+
ex_w("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;$D$D");
877+
ex_w("$D-- phpMiniAdmin dump end$D");
866878
}
867-
868-
ex_w("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;$D$D");
869-
ex_w("$D-- phpMiniAdmin dump end$D");
870879
ex_end();
871-
exit;
880+
if (!$ex_issrv) exit;
881+
$out_message='Export done successfully';
872882
}
873883

874884
function do_export_table($t='',$isvar=0,$MAXI=838860){
875-
global $D;
885+
global $D,$ex_issrv;
876886
@set_time_limit(600);
877887

878888
if($_REQUEST['s']){
@@ -897,34 +907,46 @@ function do_export_table($t='',$isvar=0,$MAXI=838860){
897907
if ($exsql) ex_w("INSERT INTO `$t` VALUES $exsql;$D");
898908
ex_w("/*!40000 ALTER TABLE `$t` ENABLE KEYS */;$D$D");
899909
}
900-
flush();
910+
if (!$ex_issrv) flush();
901911
}
902912

903913
function ex_hdr($ct,$fn){
914+
global $ex_issrv;
915+
if ($ex_issrv) return;
904916
header("Content-type: $ct");
905917
header("Content-Disposition: attachment; filename=\"$fn\"");
906918
}
907-
function ex_start(){
908-
global $ex_isgz,$ex_gz,$ex_tmpf;
919+
function ex_start($ext){
920+
global $ex_isgz,$ex_gz,$ex_tmpf,$ex_issrv,$ex_f,$DUMP_FILE;
909921
if ($ex_isgz){
910-
$ex_tmpf=tmp_name().'.gz';
922+
$ex_tmpf=($ex_issrv?export_fname($DUMP_FILE,true).$ext:tmp_name()).'.gz';
911923
if (!($ex_gz=gzopen($ex_tmpf,'wb9'))) die("Error trying to create gz tmp file");
924+
}else{
925+
if ($ex_issrv) $ex_f=fopen(export_fname($DUMP_FILE,true).$ext,'wb');
912926
}
913927
}
914928
function ex_w($s){
915-
global $ex_isgz,$ex_gz;
929+
global $ex_isgz,$ex_gz,$ex_issrv,$ex_f;
916930
if ($ex_isgz){
917931
gzwrite($ex_gz,$s,strlen($s));
918932
}else{
919-
echo $s;
933+
if ($ex_issrv){
934+
fwrite($ex_f,$s);
935+
}else{
936+
echo $s;
937+
}
920938
}
921939
}
922940
function ex_end(){
923-
global $ex_isgz,$ex_gz,$ex_tmpf;
941+
global $ex_isgz,$ex_gz,$ex_tmpf,$ex_issrv,$ex_f;
924942
if ($ex_isgz){
925943
gzclose($ex_gz);
926-
readfile($ex_tmpf);
927-
unlink($ex_tmpf);
944+
if (!$ex_issrv){
945+
readfile($ex_tmpf);
946+
unlink($ex_tmpf);
947+
}
948+
}else{
949+
if ($ex_issrv) fclose($ex_f);
928950
}
929951
}
930952

0 commit comments

Comments
 (0)