Reputation: 35
My Objective:
To delete images in a folder that are not listed in a mysql database.
I want this to be able to run when I logout of my portal.
Any idea on how to do this? Thanks
The Database
Database Name: Imagelist
id, image, thumbnail
1, image1.jpg, image1thumb.jpg
2, image2.jpg, image2thumb.jpg
3, image3.jpg, image3thumb.jpg
The Folder
Folder Name: images
image1.jpg
image1thumb.jpg
image2.jpg
random1.jpg
image2thumb.jpg
image3.jpg
mage3thumb.jpg
random2.jpg
etc...
Upvotes: 2
Views: 1001
Reputation:
Scan the folder with readdir and check each path, if this path exists in your table.
Upvotes: 0
Reputation: 1254
You can get image filename in alphabetical order using
SELECT image, thumbnail FROM imagelist ORDER BY image
and later get list of all files using scandir function of php. this will give you all files in directory by default.
then you can fetch row one by one if this matches with array value
If yes, continue to next array value and fecth row
If no, delete file with using unlink php function, then move to next array value till value matches with row value. If it matches continue as above step.
This way you can do this task in one mysql query and one dirscan.
Upvotes: 2