PHP - Good way to see if multiple files are present in a database?
I have a data开发者_如何学运维base full of image IDs, the IDs correspond to the filenames of the images stored on the server. I'd like to check if all files in the image directory are present in the database, and if not, return an array of only those files which are NOT present in the database.
Currently I'm using scandir
to obtain an array of files in the directory, then retrieving all image IDs from the SQLite database, and using array_diff
to return all the files not present already in the DB.
I don't think this is a very efficient method at all, especially when the amount of files on the server rises. I'm using PHP.
You could sort the array of file names before you query the database, and then scan this sorted array as you retrieve the results from your database query (also sorted by file name).
However, your current solution is fine as the array operation is conducted in memory and hence fairly cheap.
What you do NOT want to do is:
- Look up files on the file system individually
- Query each file in the database individually
Either of these operations is quite expensive. Just about any other solution will do fine until you get to 100,000s of files.
精彩评论