Upload CSV into database using php - php

I am working on a little project of mine and I came to the conclusion that being able to automatically upload a excel sheet to my database would be very, very useful,the problem is that I don´t have an idea of where to start, I have researched a bit and decieded to use a CSV file created from a excel sheet to upload the data into the table of my DB.
Most of the examples I have seem look like a mess with the PHP code into the html instead of dividing the logic in different files like what I have been doing in this last 2 months.
What I have right now is the upload form in html:
<form enctype="multipart/form-data" method="post" id="uploadForm">
<input name="filesfiles" id="upload" type="file" accept=".csv" class="left" />
<input type="submit" value="Cargar" />
</form>
And a small sample of how the CSV file looks in text:
Cedula;Nombre;Apellido1;Apellido2;Correo;IdRol;Estado
1657890;Dominico;Scarlatti;Viera;leetrills#yahoo.com;2;0
5657890;Franz;Listz;Linerman;flizts#hotmail.com;3;0
Or in some other excel versions:
Cedula,Nombre,Primer Apellido,Segundo Apellido,Correo,IDRol,Estado
126548791,Franz ,Ritter ,von Liszt,fliszt#arppegio.com,3,0
174657109,Sofia ,Asgatovna ,Gubaidulina ,gubaidulina#yahoo.com,3,0
The first row is the name of the columns (which should be ignored when adding the info) of the table I want to upload the file into.
The problem is that I don´t know how to link the upload file once the submit button is clicked to a PHP code in my includes that inserts the CSV into the table.
Thanks a lot in advance
EDIT:
JSFiddle of the upload form
EDIT4:
I am a stroke of pure genius and skill Maduka was able to help me solve this behemoth of problem. I can't thank him enough, the following is the code used in hopes that it may serve someone someday and save them the grief of failure.
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING & ~E_STRICT);
mysql_connect('localhost', 'root', '');
mysql_select_db("proyecto") or die(mysql_error());
if (isset($_FILES['csvupload'])) {
$errors = array();
$allowed_ext = array('.csv');
$file_name = $_FILES['csvupload']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILES['csvupload']['size'];
$file_tmp = $_FILES['csvupload']['tmp_name'];
if (in_array($allowed_ext) === false) {
$errors[] = 'La extensión del archivo no es valida.';
}
if ($file_size > 10485760) {
$errors[] = 'El archivo sobrepasa el limite de 10MB';
}
if (empty($errors)) {
$handle = fopen($file_tmp, "r");
while (!feof($handle)) {
$value = (fgetcsv($handle, 0, ','));
if ($i > 0) {
if ($value[0] != '') {
$inserts[] = "('" . mysql_real_escape_string($value[0]) . "','"
. mysql_real_escape_string($value["1"]) . "','"
. mysql_real_escape_string($value["2"]) . "','"
. mysql_real_escape_string($value["3"]) . "','"
. mysql_real_escape_string($value["4"]) . "','"
. mysql_real_escape_string($value["5"]) . "','"
. mysql_real_escape_string($value["6"]) . "')";
}
} elseif ($i == 0) {
$fields = $value;
}
$i++;
}
mysql_query("INSERT INTO `usuarios` (`cedula`,`nombre`,`apellido1`,`apellido2`,`correo`,`idRol`,`estado`) VALUES " . implode(",", $inserts));
fclose($handle);
if ($sq1) {
echo '¡Los usuarios han sido agregados exitosamente!';
}
}
}
?>

Here is the basic code which you need to do your task,
$file = fopen($_FILES['csvUpload']['tmp_name'], "r");
$i = 0;
while (!feof($file)) {
$value = (fgetcsv($file, 0, ';'));
if ($i > 0) {
if ($value[0] != '') {
$inserts[] = "(" . $value[0] . ","
. $value["1"] . ","
. $value["2"] . ","
. $value["3"] . ","
. $value["4"] . ","
. $value["5"] . ","
. $value["6"] . ")";
}
} elseif ($i == 0) {
$fields = $value;
}
$i++;
}
mysql_query("INSERT INTO `MyTable` (`" . $fields[0] . "`,`" . $fields[1] . "`,`" . $fields[2] . "`,`" . $fields[3] . "`,`" . $fields[4] . "`,`" . $fields[5] . "`) VALUES " . implode(",", $inserts));
fclose($file);
You have to implement validation, check file type and size limit. Then insert your data to the table. I have use MySQL bulk insert to handle large amount of data. Hope this helps!
EDIT 1:
Please replace your code with this code and see if it is working correctly.
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING);
mysql_connect('localhost', 'root', '');
mysql_select_db("proyecto") or die(mysql_error());
if (isset($_FILES['csvUpload'])) {
$errors = array();
$allowed_ext = array('.csv');
$file_name = $_FILES['csvUpload']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILES['csvUpload']['size'];
$file_tmp = $_FILES['csvUpload']['tmp_name'];
if (in_array($allowed_ext) === false) {
$errors[] = 'La extensión del archivo no es valida.';
}
if ($file_size > 10485760) {
$errors[] = 'El archivo sobrepasa el limite de 10MB';
}
if (empty($errors)) {
$handle = fopen($file_tmp, "r");
while (($fileop = fgetcsv($handle, ";") && fgetcsv($handle, ",")) !== false) {
$cedula = mysql_real_escape_string($fileop[0]);
$nombre = mysql_real_escape_string($fileop[2]);
$apellido1 = mysql_real_escape_string($fileop[3]);
$apellido2 = mysql_real_escape_string($fileop[4]);
$correo = mysql_real_escape_string($fileop[5]);
$idRol = mysql_real_escape_string($fileop[6]);
$estado = mysql_real_escape_string($fileop[9]);
$sq1 = mysql_query("INSERT INTO `usuarios` (cedula,nombre,apellido1,apellido2,correo,idRol,estado) VALUES ('$cedula','$nombre','$apellido1','$apellido2','$correo','$idRol','$estado')");
}
fclose($handle);
if ($sq1) {
echo '¡Los usuarios han sido agregados exitosamente!';
}
}
}
?>
<form enctype="multipart/form-data" method="post" id="uploadForm">
<input name="csvUpload" id="upload" type="file" accept=".csv" class="left" />
<input type="submit" value="¡Cargar!" />
</form>

Upload form
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<ul><li>
<input name="file" type="file" /><br /></li><li>
<br><input type="submit" name="submit" value="Upload" /></li>
</ul>
</form>
uploader.php
<?php
if (isset($_FILES['file'])) {
$errors = array();
$allowed_ext = array('csv');
$file_name = $_FILES['file']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
if (in_array($file_ext, $allowed_ext) === false) {
$errors[] ='Extension not allowed';
}
if ($file_size > 10485760) {
$errors[] = 'File size must be under 10mb';
}
if (empty($errors)) {
$handle = fopen($file_tmp,"r");
while(($fileop = fgetcsv($handle,",")) !== false)
{
$companycode = mysql_real_escape_string($fileop[0]);
$pdtcode = mysql_real_escape_string($fileop[2]);
$Item = mysql_real_escape_string($fileop[3]);
$pack = preg_replace('/[^A-Za-z0-9\. -]/', '', $fileop[4]);
$lastmonth = mysql_real_escape_string($fileop[5]);
$ltlmonth = mysql_real_escape_string($fileop[6]);
$op = mysql_real_escape_string($fileop[9]);
$pur = mysql_real_escape_string($fileop[10]);
$sale = mysql_real_escape_string($fileop[12]);
$bal = mysql_real_escape_string($fileop[17]);
$bval = mysql_real_escape_string($fileop[18]);
$sval = mysql_real_escape_string($fileop[19]);
$sq1 = mysql_query("INSERT INTO `sas` (companycode,pdtcode,Item,pack,lastmonth,ltlmonth,op,pur,sale,bal,bval,sval) VALUES ('$companycode','$pdtcode','$Item','$pack','$lastmonth','$ltlmonth','$op','$pur','$sale','$bal','$bval','$sval')");
}
fclose($handle);
if($sq1){
echo 'Stock and Sales successfully updated. Please check the values.<br><br>';
}
}
?>
The above code is simple. I am using for my project.

Related

How to bind varying number of inputs when some are blob and must be sent send_long_data() in PHP

I am working on a school assignment and I need to process a form with values such as location, price, description, and a 1 to 4 images up to 5MB each. I need to upload to a database, but I cannot get the images sent using send_long_data(). I do not know how to process only some of the inputs as send long data. I have been able to cobble together code for binding an array of inputs by reference using call_user_func_array(), but for over a day now I have had no luck getting it to work. New to coding and I am stumped. Any help is appreciated.
**<?php
//show logged in/logout header
include 'header.php';
include 'connectLankasListDB.php';
$image_count = 0;
if(!isset($_SESSION['username'])) {
echo ('Please login before posting.');
sleep(2);
header('Location: login.html');
} elseif (empty($_POST['title']) or
empty($_POST['price']) or
empty($_POST['description']) or
empty($_POST['email']) or
empty($_POST['confirm_email'])) {
//All fields not filled out
echo 'Please fill out title, price, description, email, and email-confirmation fields.';
echo '<br/>';
echo 'New Post';
//email not a valid email, prompt to enter correct email
} elseif (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
die("Enter a valid email address.");
//terms agreement checkbox not checked
} elseif ($_POST['terms'] === '0') {
die("Please agree to terms and conditions");
//email and confirmation email match, continue with script
} elseif ($_POST['email'] !== $_POST['confirm_email']) {
echo 'Email and confirmation email do not match--try again!';
//Check that image files are correct type and within the size limit
} elseif (isset($_FILES['images1'])) {
//print_r($_FILES);
$image_count = count(array_filter($_FILES['images1']['tmp_name'])); //number of uploaded images
$allowed_extensions = array("jpg", "jpeg", "png", "gif", "bmp");
for ($x=0; $x < $image_count; $x++) {
$file_name = $_FILES['images1']['name'][$x];
$file_size = $_FILES['images1']['size'][$x];
$file_tmp = $_FILES['images1']['tmp_name'];
//$ext = substr($file_name, strlen($file_name)-4,strlen($file_name));
$ext = explode(".", $file_name);
$file_ext = strtolower(end($ext));
echo $file_ext;
if (!in_array($file_ext, $allowed_extensions)) {
die("Only jpg, jpeg, png, gif, and bmp files allowed!");
} elseif ($file_size > 5000000) {
die("File size limit (5MB) exceed!");
}
}
}
//user has filled in all required fields
//validate and sanitize, store to variables
$sub_category = filter_input(INPUT_POST, 'sub_category', FILTER_SANITIZE_STRING);
$location = filter_input(INPUT_POST, 'location', FILTER_SANITIZE_STRING);
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$price = filter_input(INPUT_POST, 'price', FILTER_SANITIZE_NUMBER_FLOAT);
$description = filter_input(INPUT_POST, 'description', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$timestamp = date("Y-m-d H:i:s");
$terms = '1';
//retrieve Location_ID from Location table for Posts table
$sql1 = "SELECT Location_ID FROM Location WHERE LocationName = '$location'";
$query1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn));
$result1 = mysqli_fetch_assoc($query1);
$location_id = $result1["Location_ID"];
//retrieve Subcategory_ID from SubCategory table for Posts table
$sql2 = "SELECT SubCategory_ID FROM SubCategory WHERE SubCategoryName = '$sub_category'";
$query2 = mysqli_query($conn, $sql2);
$result2 = mysqli_fetch_assoc($query2);
$subcategory_id = $result2["SubCategory_ID"];
/*
//save to Posts table
mysqli_query($conn, "INSERT INTO Posts
(Location_ID, title, price, description, email, SubCategory_ID, TimeStamp, Agreement)
VALUES
('" . $location_id . "', '" . $title . "', '" . $price . "', '". $description . "', '" . $email ."',"
. "'" . $subcategory_id ."', '" . $timestamp ."', '" . $terms . "')")
OR die(mysqli_error($conn));*/
**//query for insert with no images
$ins_query_fields = "Location_ID, Title, Price, Description, Email, SubCategory_ID,TimeStamp, Agreement";
$ins_query_vals = "?,?,?,?,?,?,?,?";
$type_args = "ssssssss";
$bind_vars = array($location_id, $title, $price, $description, $email, $subcategory_id, $timestamp, $terms);
$tmp_name_array = array();
$pic_name_array = array();
//print_r($_FILES['images1']['tmp_name']);
//prepare query based on number of images
if ($image_count > 0) {
$i = 1;
for($n = 0; $n < $image_count; $n++) {
$ins_query_fields .= ", Image_" . $i;
array_push($pic_name_array, "Image_". $i);
$ins_query_vals .= ",?";
$type_args .= "s";
${"Image_". $i} = $_FILES['images1']['tmp_name'][$n];
array_push($tmp_name_array, ${"Image_". $i});
$i++;
}
$bind_vars = array_merge($bind_vars, $tmp_name_array);
}
**//save image files to Posts table
///////////////////////////////////////
$stmt = $conn->prepare("INSERT INTO Posts($ins_query_fields) VALUES($ins_query_vals)");
//
//bind params by reference
$inputArray[] = &$type_args;
$j = count($bind_vars);
for($i=0; $i < $j; $i++) {
$inputArray[] = &$bind_vars[$i];
}
//print_r($inputArray);
//use call_user_func_array
call_user_func_array(array($stmt, 'bind_param'), $inputArray);
//$stmt->execute();
//print_r($bind_vars);
print_r($tmp_name_array);
if ($image_count > 0) {
$index = count($bind_vars) - $image_count - 1;
for($i = 0; $i < $image_count; $i++) {
$contents = $tmp_name_array[$i];
//$fp = fopen($bind_vars[($index) + $i], "r");
$fp = fopen($contents, "r");
$size = 0;
while ($data = fread($fp, 1024)) {
$size += strlen($data);
$stmt->send_long_data($index, $data);
}
}
}
if ($stmt->execute()) {
} else {
die($conn->error);
}****
echo 'Your post has been saved.<br/>';
echo '<br/>';
echo 'Go to Main Page';
?>**
Ok, I tried to separate the data upload, which is a fixed number of variables, and the image blob uploads, which i have tried doing with a loop. The first data posts, the images do not. Is this even workable this way? There must be some fundamental principle about this that I am not understanding. Here is the revised code, omitting the validation steps.
``if ($image_count > 0) {
$i = 1;
$pic_query_holder_x = "";
$tmp_name_array = array();
$pic_in_fields = array();
$pic_type_args = "";
for($n = 0; $n < $image_count; $n++) {
//$ins_query_fields .= ", Image_" . $i;
array_push($pic_in_fields, "Image_". $i);
$pic_query_holder_x .= ",?";
$pic_type_args .= "s";
${"Image_". $i} = $_FILES['images1']['tmp_name'][$n];
array_push($tmp_name_array, ${"Image_". $i});
$i++;
}
$pic_query_holder = ltrim($pic_query_holder_x, ',');
$pic_bind_vars = $tmp_name_array;
echo '<br/>';
echo $pic_query_holder;
echo '<br/>';
print_r($tmp_name_array);
}
//save image files to Posts table
///////////////////////////////////////
$stmt = $conn->prepare("INSERT INTO Posts($ins_query_fields)
VALUES($ins_query_vals)");
//
//bind params by reference
$inputArray[] = &$type_args;
$j = count($bind_vars);
for($i=0; $i < $j; $i++) {
$inputArray[] = &$bind_vars[$i];
}
//use call_user_func_array
call_user_func_array(array($stmt, 'bind_param'), $inputArray);
$stmt->execute();
//$index = count($bind_vars) - $image_count -1;
//$fp = fopen($tmp_name_array[$index], "r");
//$stmt->execute();
//print_r($pic_in_fields);
//print_r($pic_query_holder);
if ($image_count > 0) {
//bind params
$in_array[] = &$pic_type_args;
$k = count($tmp_name_array);
for ($i=0; $i < $k; $i++) {
$in_array[] = &$tmp_name_array[$i];
}
//$index = count($tmp_name_array) - $image_count - 1;
for($i = 0; $i < $image_count; $i++) {
//prepare statement
$go_pics = $conn->prepare("INSERT INTO Posts($pic_in_fields[$i]) VALUES(?)");
$contents = $tmp_name_array[$i];
//$fp = fopen($bind_vars[($index) + $i], "r");
$fs = fopen($contents, "r");
$size = 0;
while ($data = fread($fs, 1024)) {
$size += strlen($data);
$go_pics->send_long_data($i, $data);
}
//print_r($in_array);
$go_pics->execute();
}
}
enter code here
You can try tinker with this working template below. This uses send_long_data() to upload images to database. This adds a record with columns of different datatypes in just one query.
Didn't use call_user_func_array() as I don't think it's needed.
<?php
$conn = new mysqli("127.0.0.1", "root", "", "db");
if(isset($_POST['submit'])) {
$null1 = NULL;
$null2 = NULL;
$null3 = NULL;
$null4 = NULL;
$title = isset($_POST['title']) ? $_POST['title'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$stmt = $conn->prepare("INSERT INTO Posts (Image_1, Image_2, Image_3, Image_4, title, email ) VALUES (?,?,?,?,?,?);");
$stmt->bind_param("bbbbss", $null1, $null2, $null3, $null4, $title, $email );
for($i = 0; $i < count( $_FILES['images1']['tmp_name'] ); $i++) {
if(!empty($_FILES['images1']['tmp_name'][$i])) {
$target_file = $_FILES['images1']['tmp_name'][$i];
$fp = fopen($target_file, "r");
while (!feof($fp)) {
$stmt->send_long_data($i, fread($fp, 8192));
}
fclose($fp);
unlink($_FILES['images1']['tmp_name'][$i]);
echo "Uploading image blob success!<br/>\n";
}
}
$stmt->execute();
$stmt->close();
$conn->close();
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="title" value="this is the title"/><br/>
<input type="text" name="email" value="john#yahoo.com"/><br/>
<input type="file" name="images1[]" value=""/><br/>
<input type="file" name="images1[]" value=""/><br/>
<input type="file" name="images1[]" value=""/><br/>
<input type="file" name="images1[]" value=""/><br/><br/>
<input type="submit" name="submit" value="Upload Data"/>
</form>
<?php
$stmt = $mysqli->prepare("INSERT INTO messages (message) VALUES (?)");
$null = NULL;
$stmt->bind_param("b", $null);
$fp = fopen("messages.txt", "r");
while (!feof($fp)) {
$stmt->send_long_data(0, fread($fp, 8192));
}
fclose($fp);
$stmt->execute();
?>
This is straight from the PHP manual. Feof() is just there to ensure that the file exists and was opened properly. With a proper loop, you can access the image file and insert the blob into the database. Alternatively, you can prepare a series of blobs and do your database update or insert separately. Either way, I think this code should get you in the right direction.
Just run this segment to insert or update with blob data after you handle the non blobs.
Also, this SO page may help you too. Insert Blobs in MySql databases with php

how to save image in wordpress folder while using custom template

This is my code
$actual_name = pathinfo($filename,PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$filetype=$_FILES['file']['type'];
$target="../wp-content/themes/childtheme/img/";
if($filetype=='image/jpeg' or $filetype=='image/png' or
$filetype=='image/gif')
{
$i = 1;
while(file_exists($target.$actual_name.".".$extension)){
$actual_name = $original_name.$i;
$filename = $actual_name.".".$extension;
$i++;
}
$target = $target.basename( $filename ) ;
move_uploaded_file($_FILES['file']['tmp_name'],$target);
$insert="INSERT INTO EnterSchool(SliderImg ) VALUES('".$target."' )";
if (mysqli_query($db, $insert)) {
echo "saved ";
}
else {
echo "Error: " . $insert . "" . mysqli_error($db);
}
$db->close();
}}
html
<input type="file" name="file" id="file" >

Multiple file upload and store only one file path to MySQL Database

This is simple html file code
<form action="upme.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<button type="submit">Upload</button>
</form>
This is simple PHP file code
<?php
$IMG = isset($_POST['files']) ? $_POST['files'] : array();
if (!empty($IMG))
{
$uploads_dir = 'images/';
foreach ($IMG["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $IMG["tmp_name"][$key];
$name = $IMG["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/".$name);
$name_array=mysql_real_escape_string($name);
$value_insert[] = "('" . $name_array . "')";
}
}
$values_insert = implode(',', $value_insert);
$query = "INSERT INTO upload (FILE_NAME) VALUES" . $values_insert;
$result = mysql_query($query);
}else{
echo 'empty array';
}
?>
when i use this code its give me 'empty array' error but file not upload.
<?php
$IMG = isset($_FILES['files']) ? $_FILES['files'] : array();
if (!empty($IMG))
{
$uploads_dir = 'images/';
$value_insert = '';
foreach ($IMG["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $IMG["tmp_name"][$key];
$name = $IMG["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/".$name);
$name_array=mysql_real_escape_string($name);
$value_insert .= $name_array . ",";
}
}
$values_insert = rtrim($value_insert, ',');
$query = "INSERT INTO upload (FILE_NAME) VALUES" . $values_insert;
$result = mysql_query($query);
}else{
echo 'empty array';
}
?>
here is the demo code
<?php
$IMG = isset($_FILES['files']) ? $_FILES['files'] : array();
if (!empty($IMG))
{
$uploads_dir = 'images/';
$i = 1;
foreach ($IMG["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $IMG["tmp_name"][$key];
$name = $IMG["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/".$name);
$name_array=mysql_real_escape_string($name);
if ($i == 1) {
$query = "INSERT INTO upload (FILE_NAME) VALUES" . $name_array;
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
}
$i++;
}
}
}else{
echo 'empty array';
}
?>

PHP - How to import 200k data faster? And there's 403 error

How can I import 200k data faster?
And when I importing csv (delimited by comma) file using online, I got 403 error, and it inserted 200-400 data only. Also when I try to import it using localhost (xampp) i got
"Exception EAccessViolation in module xampp-control.exe at 001AA712.
Access violation at address 005AA712 in module 'xampp-control.exe'.
Read of address 00000042"
And the SQL Database connection is gone.
This is the code I used.
set_time_limit(0);
ignore_user_abort(true);
$file = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$temp = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if( ! $file)
{
$data['error'] = "Please select a file!";
}
else if($type != "application/vnd.ms-excel" && $type != "application/octet-stream")
{
$data['error'] = "Invalid file type!";
}
else
{
$newname = $file." - ".date("Ymd His");
move_uploaded_file($temp, "uploads/".$newname);
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "uploads/".$newname;
if( ! file_exists($csvfile))
{
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if( ! $file)
{
echo "Error opening data file.";
exit;
}
$size = filesize($csvfile);
if(!$size)
{
echo "File is empty.";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$row = 1;
$data_imported = 0;
$file3 = fopen($csvfile,"r");
$total_file_count = (count(file(FCPATH."/".$csvfile)) - 2);
$i = 0;
$insert = "INSERT IGNORE INTO `invoice`
(`row1`,
.
.
to
.
.
`row33`
) VALUES ";
while($datas = fgetcsv($file3, 10000, ","))
{
$i++;
ob_implicit_flush(true);
if($row == 1)
{
// Ignore 1st line
}
else
{
$row1 = isset($datas[0]) ? $datas[0] : "";
.
.
to
.
.
$row33 = isset($datas[32]) ? $datas[32] : "";
if($i == 200 OR $total_file_count == $data_imported)
{
$insert .= "(
'".mysqli_real_escape_string($this->db->conn_id(),$row1)."',
.
.
to
.
.
'".mysqli_real_escape_string($this->db->conn_id(),$row33)."'
);";
}
else
{
$insert .= "(
'".mysqli_real_escape_string($this->db->conn_id(),$row1)."',
.
.
to
.
.
'".mysqli_real_escape_string($this->db->conn_id(),$row33)."'
),";
}
if($i == 200 OR $total_file_count == $data_imported)
{
$this->QModel->query($insert);
$i=0;
$insert = "INSERT IGNORE INTO `invoice`
(`row1`,
.
.
to
.
.
`row33`
) VALUES ";
}
$data_imported++;
}
$row++;
}
fclose($file3);
echo "Success imported ".number_format($data_imported)." data.";
Any ideas?
Thank you.

Import an excel (.csv) into MySQL using PHP code and an HTML form

I know there are other posts similar to this, but everyone recommends just doing it directly in PHPMyAdmin into MySQL (Which works perfectly, but I need to import through an HTML form to PHP to MySQL.
I would like to have an HTML form that collects a file. Then passes that file to a PHP script and I would like to know if it is possible to simply call a PHP function that converts a comma delimeted .csv file into MySQL and adds it to the database.
Or is the only way to parse the file line by line and add each record?
I haven't fully tested this, but I don't see any reason why it wouldn't work.
<?php
if ( isset( $_FILES['userfile'] ) )
{
$csv_file = $_FILES['userfile']['tmp_name'];
if ( ! is_file( $csv_file ) )
exit('File not found.');
$sql = '';
if (($handle = fopen( $csv_file, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql .= "INSERT INTO `table` SET
`column0` = '$data[0]',
`column1` = '$data[1]',
`column2` = '$data[2]';
";
}
fclose($handle);
}
// Insert into database
//exit( $sql );
exit( "Complete!" );
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CSV to MySQL Via PHP</title>
</head>
<body>
<form enctype="multipart/form-data" method="POST">
<input name="userfile" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
Of course you would need to validate the data first.
We used this awhile ago, and it works just fine. Just watch your file and directory permissions. csv_upload_mysql_conf.inc is just the DB link. This will parse multiple files at once, and put them in a table called import. Update accordingly.
<?php
/* The conf file */
include_once "csv_upload_mysql_conf.inc";
$php_self = $_SERVER['PHP_SELF'];
$file_open = 0;
$file_exts = array
( 'csv');
#Our Form.....
$form = <<< EOFFORM
<div align='center' style='border: 1px solid #CCC; background-color: #FAFAFA;padding: 10px; color: #006699; width: 620px; font-family: palatino, verdana, arial, sans-serif;' >
<table align=center style='border: 1px solid #CCC; background-color: #FFF;padding: 20px; color: #006699;' cellspacing=1><tbody>
<tr><td>
<form enctype='multipart/form-data' action='$php_self' method='post'><input type='hidden' name='MAX_FILE_SIZE' value='2000000' /><input type='hidden' name='selected' value='yes' /> Selected file: <input name='userfile[]' type='file' id='userfile[]' multiple='' onChange='makeFileList();' /><br /><br /><input type='submit' value='Upload CSV' />
</td></tr></tbody></table></div>
<p>
<strong>Files You Selected:</strong>
</p>
<ul id="fileList"><li>No Files Selected</li></ul>
<script type='text/javascript'>
function makeFileList() {
var input = document.getElementById('userfile[]');
var ul = document.getElementById('fileList');
while (ul.hasChildNodes()) {
ul.removeChild(ul.firstChild);
}
for (var i = 0; i < input.files.length; i++) {
var li = document.createElement('li');
li.innerHTML = input.files[i].name;
ul.appendChild(li);
}
if(!ul.hasChildNodes()) {
var li = document.createElement('li');
li.innerHTML = 'No Files Selected';
ul.appendChild(li);
}
}
</script>
EOFFORM;
#End Form;
if(!isset($_POST['selected'])){
echo "$form";
}
elseif($_POST['selected'] == "yes"){
$uploaddir = 'uploads/';
if(count($_FILES['userfile']['name'])) {
foreach ($_FILES['userfile']['name'] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES['userfile']['tmp_name'][$key];
$name = $_FILES['userfile']['name'][$key];
$f_type = trim(strtolower(end(explode('.', $name))));
if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");
}
$uploadfile = $uploaddir . $name;
if (! file_exists($uploadfile)) {
if (move_uploaded_file($tmp_name, $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
$flag = 1;
chmod($uploadfile, 0777);
} else {
print "File Upload Failed. ";
$flag = 0;
}
$flag = 1;
if ($flag == 1) {
echo "\n parsing Data...";
flush();
if (file_exists($uploadfile)) {
$fp = fopen($uploadfile, 'r') or die (" Can't open the file");
$fileopen = 1;
$length = calculate_length($uploadfile);
}
$replace = "REPLACE";
$field_terminater = ",";
$enclose_option = 1;
$enclosed = '"';
$escaped = '\\\\';
$line_terminator = 1;
$local_option = 1;
$sql_query = 'LOAD DATA';
if ($local_option == "1") {
$sql_query .= ' LOCAL';
}
$sql_query .= ' INFILE \'' . $uploadfile . '\'';
if (!empty($replace)) {
$sql_query .= ' ' . $replace;
}
$sql_query .= ' INTO TABLE ' . "`import`";
if (isset($field_terminater)) {
$sql_query .= ' FIELDS TERMINATED BY \'' . $field_terminater . '\'';
}
if (isset($enclose_option) && strlen($enclose_option) > 0) {
$sql_query .= ' OPTIONALLY';
}
if (strlen($enclosed) > 0) {
$sql_query .= ' ENCLOSED BY \'' . $enclosed . '\'';
}
if (strlen($escaped) > 0) {
$sql_query .= ' ESCAPED BY \'' . $escaped . '\'';
}
if (strlen($line_terminator) > 0){
$sql_query .= ' LINES TERMINATED BY \'' . '\r\n' . '\'';
}
$result = mysql_query ($sql_query);
echo mysql_error() ;
if(mysql_affected_rows() > 1) {
echo " <div align=center><b><font color=#66CC33>The csv data was added.</font></div> ";
}
else {
error_log(mysql_error());
echo " <div align=center><b><font color=#E96B10> Couldn't enter the data to db </font></div>";
}
if ($file_open ==1) {
fclose($fp) or die("Couldn't close the file");
}
}
}
}
echo "<meta http-equiv='refresh' content='0; url=index.php'>";
}
}
function calculate_length($fp) {
$length = 1000;
$array = file($fp);
for($i=0;$i<count($array);$i++)
{
if ($length < strlen($array[$i]))
{
$length = strlen($array[$i]);
}
}
unset($array);
return $length;
}
?>
Everything in your code is fine, only mistake is you are not using mysql_query for inserting the data into table. Mysql query not running in your script. corrected code follows...
<?php
if ( isset( $_FILES['userfile'] ) )
{
$csv_file = $_FILES['userfile']['tmp_name'];
if ( ! is_file( $csv_file ) )
exit('File not found.');
$sql = '';
if (($handle = fopen( $csv_file, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = mysql_query("INSERT INTO `table` SET
`column0` = '$data[0]',
`column1` = '$data[1]',
`column2` = '$data[2]';
");
}
fclose($handle);
}
// Insert into database
//exit( $sql );
exit( "Complete!" );
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CSV to MySQL Via PHP</title>
</head>
<body>
<form enctype="multipart/form-data" method="POST">
<input name="userfile" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>

Categories