What is the fastest way to check if files in a folder exist in my mysql database?
Question
I want to scan a folder of effects and check if each file is listed as a row in my MySQL database of effects, and if it isn't I will show it as an un-tracked file. Does anyone know an efficient method to achieve this with PHP? thank you very much.
Solution
I think there's only one way: glob
the directory, prepare two statements (a check and an update) using PDO or mysqli and loop through the files.
If you need to do it on a regular basis, you should move the tracked files to a different place as soon as you track / check them.
OTHER TIPS
The best way, that i can think of, would be to store registration_date
along with the filename
in the table.
- select latest updated/added file from DB, get the timestamp for that file
- create a prepared statement for adding a filename to the table
- loop through the the list of file in your directory looking for file which are newer then the timestamp
- insert each filename, which fits the conditions, and current timestamp into the table
- if there is violation of
UNIQUE
constraint, it means that file has been updated, not added.
IMHO, this would be the most effective way to do this.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow