开发者

Deleting image in folder that are NOT listed in a mysql database via PHP

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...


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.


Scan the folder with readdir and check each path, if this path exists in your table.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜