开发者

how to synchronize database table and directory with php

I have a directory with files and a database table with what should be the same files. I would like to be able to synchronize the database table with the directory. What would be the most efficient way to do this? or would I realistically only be able to do this in a brute manner?

Here's my approach:

1. retrieve all of the files in the directory as array

2. retrieve all of the filenames in the database table as array

3. loop through the file开发者_高级运维 values in the directory array and use in_array() on the database table array to verify the filename is in that array, and if not then start building an array to insert the missing filenames. run db query to add each missing file row to database table

4. loop through directory array and use in_array() on the directory array and anything not found in the directory array will just be deleted from the table.

Is there a better way to go about this? or something better for this in php than in_array()?


You can use array_diff() to efficiently determine the differences between two arrays. Run it one way to get the things that have been added, and the other way (swap the arguments) for the things that have been deleted.

(Note, however, that you may want to operate on copies of the arrays, not originals.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜