Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import .sql database files from a folder into Adminer.
<?php
/**
* Import databases from a folder
*
* @author joshcangit, http://github.com/joshcangit/
*/
class AdminerImportFromFolder {
private $folder;
/**
* $folder - directory containing .sql files
*/
function __construct($folder){
$this->folder = $folder;
}
function importServerPath() {
?> <select name="file">
<option value=""></option>
<?php
// load all .sql files
foreach (glob($this->folder."/*.sql") as $path) {
$file = preg_replace("~.*\/(.*\.sql)~", "$1", $path);
?> <option value="<?php echo $path; if ($_POST['file'] == $path) return $_POST['file']; ?>"><?php echo $file; ?></option>
<?php
}
?>
</select>
<?php
}
}
?>
@joshcangit

This comment has been minimized.

Copy link
Owner Author

joshcangit commented Jan 5, 2019

Introduction

I know there would be an error if I uploaded a file for importing that is too large.
I like that I can import .sql database files from a folder like how it is in phpMyAdmin which avoids the upload file size limit problem.
In phpMyAdmin, I can change this setting in config.inc.php. e.g. The folder is called upload.

/**
 * Directories for saving/loading files from server
 */
$cfg['UploadDir'] = 'upload';

After that, I made a folder called upload in the phpmyadmin folder and store my .sql files there for easy and convenient importing with no file size limit.

This plugin aims to do just that on Adminer. I find the way it is done on Adminer to be not as convenient. Needing to make a file in the same place as adminer.php called adminer.sql and copy-pasting all the .sql files into that 1 file. I like it in separate files.

Usage

Make a folder to store .sql files for uploading preferably the same place as adminer.php.
e.g. The folder is called upload.

- plugins
	- folder-import.php
	- ...
	- plugin.php
- upload
        - ....sql
        - ....sql
        - ...
- adminer.php
- index.php

Instructions referenced from https://www.adminer.org/en/plugins/#use

<?php
function adminer_object() {
    // required to run any plugin
    include_once "./plugins/plugin.php";

    // autoloader
    foreach (glob("plugins/*.php") as $filename) {

        include_once "./$filename";
    }

    $plugins = array(
        // specify enabled plugins here
        new AdminerImportFromFolder("upload"), // This is where and how to use the plugin.
    );

    return new AdminerPlugin($plugins);
}

// include original Adminer or Adminer Editor
include "./adminer.php";
?>

How it looks

On selecting a file. In this case, it is a file called new_database.sql made to test this out.
import-new-database
After clicking Run file, the page will look pretty messed up.
page-looks-messed-up
Even so, the new database works just fine.
new-database

@joshcangit

This comment has been minimized.

Copy link
Owner Author

joshcangit commented Jan 18, 2019

Suggestions for improvements are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment