1

I have a process where an .csv file is sent to a server to update the database with the information on csv.

The first column in .csv file has is a date and there is always only to different dates in the file.

Is there a way to identify those 2 dates that comes in the file? I need to know which are those dates in order to delete it from the database and then make the update with the new information.

This is the function I have to read the file:

  function query_frag($line){

$frag = str_getcsv($line,"",',',"\\");

        if(count($frag) < 3)
            return false;

        $error_date = preg_replace('/\//','-',$frag[0]);
        $error_monitoring_page = trim($frag[1]);
        $quantity = (int)$frag[2];
        $error_code = trim(preg_replace('/[^A-Za-z0-9\-]/', '', substr($frag[1], -6)));
        $date_added = 'now()';

        if(($quantity == 0) || ($error_date == ''))
            return false;


        $query = "('$error_monitoring_page','$error_code','$quantity','$error_date',$date_added),";

        return $query; }

Then I read the file in folder with following code:

    $path = "../received_files/";

$files = array();

if (!is_dir($path)) {
    exit('Invalid diretory path');
}

foreach (scandir($path) as $file) { 
    if ('.' === $file) continue;
    if ('..' === $file) continue;

$file_name = '../received_files/'.$file;
$name = $file;

$blob = file_get_contents($file_name);
$lines = explode("\n",trim($blob));

$batch_size = 100;

$total = count($lines);

Then comes the code to update the database but before that I need to delete all records that match the dates in file.

How to do that?

  • Possible duplicate of [Working with file pointers on a csv](http://stackoverflow.com/questions/41294380/working-with-file-pointers-on-a-csv) – e4c5 Dec 27 '16 at 10:13

0 Answers0