mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
Fix CSV import #175
This commit is contained in:
@@ -11,10 +11,11 @@
|
|||||||
// External files
|
// External files
|
||||||
require dirname(__FILE__).'/init.php';
|
require dirname(__FILE__).'/init.php';
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Action selector
|
// Action selector
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Set maximum execution time to 15 seconds
|
// Set maximum execution time to 15 seconds
|
||||||
|
|
||||||
ini_set ('max_execution_time','30');
|
ini_set ('max_execution_time','30');
|
||||||
|
|
||||||
// Action functions
|
// Action functions
|
||||||
@@ -478,34 +479,35 @@ function ImportCSV() {
|
|||||||
|
|
||||||
// sql
|
// sql
|
||||||
$sql = 'DELETE FROM Devices';
|
$sql = 'DELETE FROM Devices';
|
||||||
|
|
||||||
// execute sql
|
// execute sql
|
||||||
$result = $db->query($sql);
|
$result = $db->query($sql);
|
||||||
|
|
||||||
// Open the CSV file with read-only mode
|
$data = file_get_contents($file);
|
||||||
$csvFile = fopen($file, 'r');
|
$data = explode("\n", $data);
|
||||||
|
|
||||||
// Skip the first line
|
|
||||||
fgetcsv($csvFile);
|
|
||||||
|
|
||||||
$columns = getDevicesColumns();
|
$columns = getDevicesColumns();
|
||||||
|
|
||||||
// Parse data from CSV file line by line (max 10000 lines)
|
$rowArray = array();
|
||||||
while (($row = fgetcsv($csvFile, 10000, ",")) !== FALSE)
|
|
||||||
|
// Parse data from CSV file line by line (max 10000 lines)
|
||||||
|
foreach($data as $row)
|
||||||
{
|
{
|
||||||
$sql = 'INSERT INTO Devices ('.implode(',', $columns).') VALUES ("' .implode('","', $row).'")';
|
// Check if not empty and skipping first line
|
||||||
|
if(count(explode(',',$row)) == count($columns) && explode(',',$row)[0] != "\"dev_MAC\"")
|
||||||
$result = $db->query($sql);
|
{
|
||||||
|
$sql = "INSERT INTO Devices (".implode(',', $columns).") VALUES (" . $row.")";
|
||||||
// check result
|
$result = $db->query($sql);
|
||||||
if ($result != TRUE) {
|
|
||||||
$error = $db->lastErrorMsg();
|
// check result
|
||||||
// break the while loop on error
|
if ($result != TRUE) {
|
||||||
break;
|
$error = $db->lastErrorMsg();
|
||||||
}
|
// break the while loop on error
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close opened CSV file
|
|
||||||
fclose($csvFile);
|
|
||||||
|
|
||||||
if($error == "")
|
if($error == "")
|
||||||
{
|
{
|
||||||
@@ -515,7 +517,7 @@ function ImportCSV() {
|
|||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
// an error occurred while writing to the DB, display the last error message
|
// an error occurred while writing to the DB, display the last error message
|
||||||
echo lang('BackDevices_DBTools_ImportCSVError')."\n\n$sql \n\n".$error;
|
echo lang('BackDevices_DBTools_ImportCSVError')."\n\n$sql \n\n".$result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ require dirname(__FILE__).'/../templates/timezone.php';
|
|||||||
require dirname(__FILE__).'/db.php';
|
require dirname(__FILE__).'/db.php';
|
||||||
require dirname(__FILE__).'/util.php';
|
require dirname(__FILE__).'/util.php';
|
||||||
require dirname(__FILE__).'/../templates/language/lang.php';
|
require dirname(__FILE__).'/../templates/language/lang.php';
|
||||||
|
?>
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ elseif ($FUNCTION == 'cleanLog')
|
|||||||
cleanLog($SETTINGS);
|
cleanLog($SETTINGS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Formatting data functions
|
// Formatting data functions
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -426,6 +425,4 @@ function setCache($key, $value, $expireMinutes = 5) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user