4
4
5
5
//########################################
6
6
7
- $ registerNew = true ; //Turn this to false if you dont want to register a new password
7
+ $ registerNew = false ; //Turn this to false if you dont want to register a new password
8
8
$ dir_path = "./ " ; //This is for the directory the backup.php lies in.
9
9
$ correct_password = "827ccb0eea8a706c4c34a16891f84e7b " ; // pass=12345, Change this to your actual password as an MD5 hash
10
10
@@ -91,7 +91,7 @@ function unzipFile($zipFile)
91
91
preventUnAuthorized ();
92
92
$ zip = new ZipArchive ;
93
93
if ($ zip ->open ($ zipFile ) === TRUE ) {
94
- $ zip ->extractTo ($ dir_path );
94
+ $ zip ->extractTo ($ dir_path );
95
95
$ zip ->close ();
96
96
echo '<div class="alert alert-success" role="alert">File unzipped successfully!</div> ' ;
97
97
} else {
@@ -154,34 +154,37 @@ function unzipFile($zipFile)
154
154
$ _SESSION ['set_dbname ' ] = $ db_name ;
155
155
156
156
$ rootPath = realpath ($ dir_path );
157
-
158
157
$ sqlfile = doSqlBackup ($ db_host , $ db_user , $ db_pass , $ db_name );
159
- $ archive_file_name = "backup- " . time () . ".zip " ;
158
+ $ archive_file_name = "./ backup- " . time () . ".zip " ;
160
159
161
- // Initialize archive object
162
160
$ zip = new ZipArchive ();
163
- $ zip ->open ($ archive_file_name , ZipArchive::CREATE | ZipArchive::OVERWRITE );
164
-
165
- // Create recursive directory iterator
166
- /** @var SplFileInfo[] $files */
167
- $ files = new RecursiveIteratorIterator (
168
- new RecursiveDirectoryIterator ($ rootPath ),
169
- RecursiveIteratorIterator::LEAVES_ONLY
170
- );
171
-
172
- foreach ($ files as $ name => $ file ) {
173
- // Skip directories (they would be added automatically)
174
- if (!$ file ->isDir ()) {
175
- // Get real and relative path for current file
176
- $ filePath = $ file ->getRealPath ();
161
+ if ($ zip ->open ($ archive_file_name , ZipArchive::CREATE | ZipArchive::OVERWRITE ) !== true ) {
162
+ throw new RuntimeException ("Cannot open < $ archive_file_name> " );
163
+ }
164
+
165
+ function addFilesToZip ($ path , $ zip , $ rootPath )
166
+ {
167
+ $ files = scandir ($ path );
168
+ foreach ($ files as $ file ) {
169
+ if ($ file === '. ' || $ file === '.. ' )
170
+ continue ;
171
+
172
+ $ filePath = $ path . DIRECTORY_SEPARATOR . $ file ;
177
173
$ relativePath = substr ($ filePath , strlen ($ rootPath ) + 1 );
178
174
179
- // Add current file to archive
180
- $ zip ->addFile ($ filePath , $ relativePath );
175
+ if (is_dir ($ filePath )) {
176
+ addFilesToZip ($ filePath , $ zip , $ rootPath );
177
+ } else {
178
+ $ zip ->addFile ($ filePath , $ relativePath );
179
+ }
181
180
}
182
181
}
182
+
183
+ addFilesToZip ($ rootPath , $ zip , $ rootPath );
184
+
183
185
$ zip ->close ();
184
186
187
+
185
188
header ("Content-type: application/zip " );
186
189
header ("Content-Disposition: attachment; filename= $ archive_file_name " );
187
190
header ("Content-length: " . filesize ($ archive_file_name ));
@@ -360,13 +363,16 @@ function doSqlBackup($host, $user, $pass, $dbname)
360
363
body {
361
364
height: 100%;
362
365
}
366
+
363
367
.content-wrap {
364
368
min-height: 100%;
365
369
margin-bottom: -100px;
366
370
}
371
+
367
372
.footer {
368
373
height: 100px;
369
374
}
375
+
370
376
.alert {
371
377
position: absolute;
372
378
top: 30px;
@@ -398,7 +404,7 @@ function doSqlBackup($host, $user, $pass, $dbname)
398
404
<div class="card">
399
405
<div class="card-header"><i class="fas fa-sign-in-alt"></i> Login</div>
400
406
<div class="card-body">
401
- <form action="<?= $ _SERVER ['PHP_SELF ' ]?> " method="post">
407
+ <form action="<?= $ _SERVER ['PHP_SELF ' ] ?> " method="post">
402
408
<div class="form-group">
403
409
<label for="password">Password:</label>
404
410
<input type="password" name="password" id="password" class="form-control" required>
@@ -414,7 +420,7 @@ function doSqlBackup($host, $user, $pass, $dbname)
414
420
<div class="card">
415
421
<div class="card-header"><i class="fas fa-user-plus"></i> Register</div>
416
422
<div class="card-body">
417
- <form action="<?= $ _SERVER ['PHP_SELF ' ]?> " method="post">
423
+ <form action="<?= $ _SERVER ['PHP_SELF ' ] ?> " method="post">
418
424
<div class="form-group">
419
425
<label for="password">New Password:</label>
420
426
<input type="password" name="newpassword" id="password" class="form-control" required>
@@ -436,7 +442,7 @@ function doSqlBackup($host, $user, $pass, $dbname)
436
442
<div class="card mt-3">
437
443
<div class="card-header"><i class="fas fa-cloud-upload-alt"></i> Backup</div>
438
444
<div class="card-body">
439
- <form action="<?= $ _SERVER ['PHP_SELF ' ]?> " method="post">
445
+ <form action="<?= $ _SERVER ['PHP_SELF ' ] ?> " method="post">
440
446
<!-- Database Credentials -->
441
447
<div class="form-group">
442
448
<label for="db_host">Database Host:</label>
@@ -478,7 +484,7 @@ class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
478
484
<div class="card mt-3">
479
485
<div class="card-header"><i class="fas fa-file-archive"></i> Unzip</div>
480
486
<div class="card-body">
481
- <form action="<?= $ _SERVER ['PHP_SELF ' ]?> " method="post">
487
+ <form action="<?= $ _SERVER ['PHP_SELF ' ] ?> " method="post">
482
488
<?php
483
489
484
490
$ zip_files = glob ('*.zip ' );
@@ -508,7 +514,7 @@ class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
508
514
<div class="card mt-3">
509
515
<div class="card-header"><i class="fas fa-database"></i> SQL Import</div>
510
516
<div class="card-body">
511
- <form action="<?= $ _SERVER ['PHP_SELF ' ]?> " method="post">
517
+ <form action="<?= $ _SERVER ['PHP_SELF ' ] ?> " method="post">
512
518
<?php
513
519
514
520
$ sql_files = glob ('*.sql ' );
@@ -547,7 +553,7 @@ class="btn mb-1 btn-success"><i class="fas fa-cloud-upload-alt"></i> Backup all
547
553
548
554
<div class="card-header"><i class="fas fa-upload"></i> Upload</div>
549
555
<div class="card-body">
550
- <form action="<?= $ _SERVER ['PHP_SELF ' ]?> " method="post" enctype="multipart/form-data">
556
+ <form action="<?= $ _SERVER ['PHP_SELF ' ] ?> " method="post" enctype="multipart/form-data">
551
557
<div class="form-group">
552
558
<label for="file">Choose File:</label>
553
559
<input type="file" name="file" id="file" class="form-control-file">
0 commit comments