I need to upload a large CSV to MySQL, if uploading breaks I have to start from where I stopped, no duplicate entries. If process breaks then it should restart from where I ended automatically (i.e.: if uploading breaK after 123 entries then it should resume from 124 on the next run)
CSV file format:
latitude longitude
6.5486 72.456
4.2186 74.466
5.5486 82.956
I only need one entry with same latitude and longitude, currently I'm using the code below (working) but I don't know how to start from the breaking point if uploading breaks.
<?php
error_reporting(0);
require("connection.php");//connect to the database
if ($_FILES[csv][size] > 0){
//get the csv file
$file = $_FILES[csv][tmp_name];
echo $fname = $_FILES['csv']['name'];
echo $ftype = end(explode('.', strtolower($fname)));
if($ftype=="csv"){
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$latitude=$data[0];
$longitude=$data[1];
$location1=$data[2];
$location2=$data[3];
$location3=$data[4];
$sql = "SELECT * FROM latitude_longitude WHERE latitude ='$latitude' AND longitude='$longitude' ";
$result=mysql_query($sql);
if( mysql_num_rows($result) > 0){
mysql_query("UPDATE latitude_longitude SET latitude = '$latitude',longitude = '$longitude',location1='$location1', location2='$location2',location3='$location3',status=status+1 WHERE latitude = '$latitude' AND longitude = '$longitude'");
}
else{
mysql_query("INSERT INTO latitude_longitude (latitude, longitude, location1, location2, location3, status, date) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'1',
CURRENT_TIMESTAMP
)
");
}
}
} while ($data = fgetcsv($handle,1000,",","'"));
//redirect
header('Location:GeoLocation.php?success=1'); die;
}else{
header('Location:GeoLocation.php?success=2'); die;
}
}
?>
thanks for helping in advance.