I have multiple text files in one of my folder. I want to write those multiple text files' data to one file and delete those multiple text files. I do it continuously within intervals. I am using a bat file. For the first time it runs correctly. It write data and also correctly delete files. But from the second time it gives me error because of I delete those files but code don't check for empty directory within second run. But let's think there are no files in the dir in the first run. Then it check and tell me there are no data. But that is not check in the second run.
This is my code.
/* Data loading interval in seconds */
$interval = 10;
/* Log file name and location */
$log_file = "C:/xampp/htdocs/attendance/file/res/result.TXT";
/* Multiple text files' location */
$files = glob("C:/xampp/htdocs/attendance/file/temp_file/*.txt");
/* Event Number and Event Description */
$EventLog = array();
$EventLog['00'] = "Check-In";
$EventLog['01'] = "Check-Out";
$EventLog['02'] = "Breack-Out";
$EventLog['03'] = "Resume-In";
$EventLog['04'] = "OT-In";
$EventLog['05'] = "OT-Out";
// Connect to mysql
$connect = mysql_connect($hostname, $username, $password) or die("connection failed");
mysql_select_db($database) or die("db selection failed");
mysql_query("CREATE TABLE IF NOT EXISTS log (user_id varchar(30) NOT NULL, date_time datetime NOT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS tmp_log (user_id varchar(30) NOT NULL, date_time datetime NOT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS process_log (user_id varchar(30) NOT NULL, check_in datetime DEFAULT NULL, check_out datetime DEFAULT NULL, flag char(1) DEFAULT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS error_log (user_id varchar(30) NOT NULL, date_time datetime NOT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS attendance_log (user_id varchar(30) NOT NULL, check_in datetime NOT NULL, check_out datetime NOT NULL)");
while(1){
/* Combine multiple text files to one */
if(count($files)==0) {
echo date("Y-m-d H:i:s")." No Files. Empty Dir.\n";
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." No Files. Empty Dir.";
ConnectionErrorLog($err_msg);
} else {
$is_loaded = TRUE;
echo date("Y-m-d H:i:s")."Multiple Files Loaded.\n";
foreach($files as $file) {
$content = file_get_contents($file);
file_put_contents($log_file, $content.PHP_EOL, FILE_APPEND);
}
if(!file_put_contents($log_file, $content.PHP_EOL, FILE_APPEND)){
$is_loaded = FALSE;
}
//if($is_loaded == TRUE){
$scan_files = scandir("C:/xampp/htdocs/attendance/file/temp_file/");
$source = "C:/xampp/htdocs/attendance/file/temp_file/";
$destination = "C:/xampp/htdocs/attendance/file/back/";
foreach ($scan_files as $s_file) {
if (in_array($s_file, array(".",".."))) continue;
if (copy($source.$s_file, $destination.date("Y_m_d_H_i_s_").$s_file.".txt")) {
$delete[] = $source.$s_file;
}
}
foreach ($delete as $s_file) {
unlink($s_file);
}
//}
}
if (!$connect) {
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." MySQL Connection Failed: ".mysql_error();
ConnectionErrorLog($err_msg);
//Connect again for this cycle.
$connect = mysql_connect($hostname, $username, $password, $database);
}
clearstatcache();
if(file_exists($log_file) && filesize($log_file)){
$is_loaded = TRUE;
if($read_file = fopen($log_file, "r")) {
while($line = fgets($read_file)) {
$line = trim($line);
$data = explode(" ", $line);
if(count($data) == 3){
$query = "INSERT INTO tmp_log (user_id, date_time) VALUES ('".$data[0]."', STR_TO_DATE('".$data[1]."', '%Y-%m-%d-%H:%i:%s'))";
if(!mysql_query($query)){
$is_loaded = FALSE;
}
}
}
// Clear file
fclose($read_file);
if($is_loaded == TRUE){
//# Remove File
//unlink($log_file);
//# Rename file and move to backup folder
$new_name = "C:/xampp/htdocs/attendance/file/back/res/result_".date("Ymd_His").".txt";
rename($log_file, $new_name);
echo date("Y-m-d H:i:s")." Result File Loaded.\n";
}else{
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." Error in Loading Data.";
ConnectionErrorLog($err_msg);
//Check and reconnect again for next cycle.
if (!mysql_ping($connect)) {
$connect = mysql_connect($hostname, $username, $password, $database);
}
}
}
} else {
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." attendance_test log file not found.";
ConnectionErrorLog($err_msg);
}
//Filter data
handle_crossover();
update_log();
sleep($interval);
}
# Close the connection
mysql_close($connect);
This is the error.
2016-10-24 11:55:38Multiple Files Loaded.
2016-10-24 11:55:38 Result File Loaded.
2016-10-24 11:55:48Multiple Files Loaded.
PHP Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.
txt): failed to open stream: No such file or directory in C:\xampp\htdocs\attend
ance\final2.php on line 54
Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.txt):
failed to open stream: No such file or directory in C:\xampp\htdocs\attendance\
final2.php on line 54
PHP Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.
txt): failed to open stream: No such file or directory in C:\xampp\htdocs\attend
ance\final2.php on line 54
Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.txt):
failed to open stream: No such file or directory in C:\xampp\htdocs\attendance\
final2.php on line 54
PHP Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.
txt): failed to open stream: No such file or directory in C:\xampp\htdocs\attend
ance\final2.php on line 54
Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.txt):
failed to open stream: No such file or directory in C:\xampp\htdocs\attendance\
final2.php on line 54
PHP Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.txt): No su
ch file or directory in C:\xampp\htdocs\attendance\final2.php on line 74
Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.txt): No such fi
le or directory in C:\xampp\htdocs\attendance\final2.php on line 74
PHP Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.txt): No su
ch file or directory in C:\xampp\htdocs\attendance\final2.php on line 74
Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.txt): No such fi
le or directory in C:\xampp\htdocs\attendance\final2.php on line 74
PHP Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.txt): No su
ch file or directory in C:\xampp\htdocs\attendance\final2.php on line 74
Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.txt): No such fi
le or directory in C:\xampp\htdocs\attendance\final2.php on line 74
2016-10-24 11:55:48 Result File Loaded.