Last active
August 29, 2015 14:07
-
-
Save FranciscoG/be0b82ca49dca7913d9d to your computer and use it in GitHub Desktop.
Just a simple file I created to upload and parse a CSV and insert that data into a MySQL db. mysql_* functions are deprecated, should use mysqli_* instead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 'on'); | |
class DbClass | |
{ | |
// MAMP default credentials for testing. These are not actually real creds that mean anything | |
private $db_url = 'localhost'; | |
private $db_login = 'root'; | |
private $db_pw = 'root'; | |
private $db_name = "db1"; | |
private $db_tablename = 'table1'; | |
private $col_name = 'id'; | |
public $uploaded = false; | |
public $info = false; | |
public $error = false; | |
public function __construct() { | |
$this->con = mysql_connect($this->db_url, $this->db_login, $this->db_pw, true); | |
if (!$this->con) { | |
$this->error = 'Could not connect: ' . mysql_error(); | |
} | |
$this->db_selected = mysql_select_db($this->db_name, $this->con); | |
if (!$this->db_selected) { | |
$this->error = 'Can\'t use that db : ' . mysql_error(); | |
} | |
} | |
public function __destruct() { | |
mysql_close($this->con); | |
} | |
public function insertDB($csv_arr) { | |
for($i = 0; $i < count($csv_arr); $i++) { | |
mysql_query("INSERT INTO ".$this->db_tablename." (".$this->col_name.") VALUES (". $csv_arr[$i] .")"); | |
} | |
} | |
public function readDB(){ | |
$r = mysql_query('SELECT * FROM ' . $this->db_tablename); | |
while($row = mysql_fetch_array($r)) { | |
echo $row[$this->col_name] . '<br>'; | |
} | |
} | |
} | |
class CSVer | |
{ | |
public function __construct($file){ | |
$this->file = $file; | |
} | |
public function isCSV(){ | |
$type = explode(".", $this->file['name']); | |
return (strtolower(end($type)) == 'csv') ? true : false; | |
} | |
public function processCSV(){ | |
$csv_filename = $this->file["tmp_name"]; | |
$_arr = array(); | |
$f = fopen($csv_filename, 'r'); | |
while (($line = fgetcsv($f)) !== FALSE) { | |
$_arr[] = $line[0]; | |
} | |
fclose($f); | |
return $_arr; | |
} | |
public function fileMeta(){ | |
$info = "Upload: " . $this->file["name"] . "<br>"; | |
$info .= "Type: " . $this->file["type"] . "<br>"; | |
$info .= "Size: " . ($this->file["size"] / 1024) . " Kb <br>"; | |
$info .= "Temp file: " . $this->file["tmp_name"] . "<br><br>"; | |
return $info; | |
} | |
} | |
class Uploader extends DbClass | |
{ | |
public $submitted = false; | |
public function __construct(){ | |
parent::__construct(); | |
if ( isset($_POST["submit"]) ) { | |
$this->submitted = true; | |
} | |
} | |
public function getInfo() { | |
return $this->info; | |
} | |
public function getError() { | |
return $this->error; | |
} | |
public function getCSVarr() { | |
return $this->csvArr; | |
} | |
public function getFile(){ | |
if ($this->submitted){ | |
if ( isset($_FILES["file"])) { | |
//if there was an error uploading the file | |
if ($_FILES["file"]["error"] > 0) { | |
$this->error = "Return Code: " . $_FILES["file"]["error"]; | |
} else { | |
$process = new CSVer($_FILES["file"]); | |
$this->info = $process->fileMeta(); | |
if (!$process->isCSV()) { | |
$this->error = "This is not a CSV file"; | |
} else { | |
$this->csvArr = $process->processCSV(); | |
$this->insertDB($this->csvArr); | |
} | |
} | |
} else { | |
$this->error = "No file selected"; | |
} | |
} | |
} | |
} | |
$begin = new Uploader; | |
$begin->getFile(); | |
?> | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title></title> | |
<!-- Bootstrap CSS for some basic styling, why not --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<br> | |
<div class="row"> | |
<div class="panel panel-primary"> | |
<div class="panel-heading">CSV Uploader</div> | |
<div class="panel-body"> | |
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data"> | |
<div class="form-group file user_basic_file"> | |
<label class="file control-label" for="file">File</label> | |
<div> | |
<input class="file" name="file" id="file" type="file"> | |
<p class="help-block">Upload CSV file only</p> | |
</div> | |
</div> | |
<input class="btn btn-default" name="submit" type="submit" value="upload"> | |
</form> | |
</div> | |
</div> | |
</div> | |
<br> | |
<div class="row"> | |
<?php if ($begin->getError()): ?> | |
<div class="panel panel-danger"> | |
<div class="panel-heading">ERROR</div> | |
<div class="panel-body"> | |
<p> | |
<?php echo $begin->getError(); ?> | |
</p> | |
</div> | |
</div> | |
<?php endif; ?> | |
<?php if ($begin->getInfo()): ?> | |
<div class="panel panel-success"> | |
<div class="panel-heading">File successfully uploaded</div> | |
<div class="panel-body"> | |
<p> | |
<?php echo $begin->getInfo(); ?> | |
</p> | |
<p> | |
<?php print_r($begin->getCSVarr()); ?> | |
</p> | |
</div> | |
</div> | |
<?php endif; ?> | |
<div class="panel panel-info"> | |
<div class="panel-heading">DB info</div> | |
<div class="panel-body"> | |
<?php | |
$begin->readDB(); | |
?> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment