I have to write a program which checks if a particular directory on my folder has any files (of a specific extension), and if it finds any files, it reads them one by one and loads data from them into a database.
This is the rough algorithm in my mind:
Using an infinite
while()loop, continuously keep checking if the directory has any files of that particular extension (e.g. check if the directory has any*.xmlfiles). I can use the PHPglob()function.If yes, then in a
foreachloop, read data from each file and load it into the database.Once a file's data has been loaded, delete it.
My Question:
I will be constantly checking if there any .xml files in the directory. This means that many times I will get a true (meaning/saying "Yes, there are .xml files in the directory") even for the files whose data is BEING loaded.
So once a file has been found in the directory, I need a check which checks if its data is in the process of being loaded into a database. How do I check that?
The process of data-loading is that I extract useful data from the file into a .csv file and then use LOAD DATA INFILE SQL query to load the data into my MySQL database.
LOAD...) is reading a file, I think you're going to need to look at semaphores or mutexes. Lockfiles are a kind of semaphore..csvand then MySQLLOAD...is executed. I want to check if the file is BEING CONVERTED TO .csv. The file is converted to .csv file through a SHELL command (the commands we execute in Windows cmd or Linux terminal).whileloop continuously checks if there is a.xmlfile in the directory. Now say, at this very moment, it finds a.xmlfile in the directory, and I start executing the shell command which extracts useful data from this.xmlfile and inserts that into a.csvfile. (After the data is extracted from .xml and dumped into .csv file, the .xml file is deleted). Now since we are in an infinite while loop (which keeps checking if there is a.xmlfile in the directory), - continued in next comment!