Delete a file based on the file records in the database

Source: Internet
Author: User
Delete a file based on the file records in the database
Check whether the fields in the database correspond to the files in the folder. if there is no corresponding field, delete the file, which greatly reduces the usage of system resources.

  1. // Created on 2010-4-24 Made by Wolf QQ116311316
  2. // Function: determines whether the fields in the database correspond to the files in the folder.
  3. // Delete the file, greatly reducing the usage of system resources.
  4. // $ Dir is the Directory of the file to be searched
  5. // $ Table: The table to be searched
  6. // $ Field is the field name
  7. // ==================================
  8. Function delfile ($ dir = "", $ table, $ field ){
  9. // Obtain files in the directory
  10. If (is_dir ($ dir )){
  11. If ($ dh = opendir ($ dir )){
  12. While ($ file = readdir ($ dh ))! = False ){
  13. If ($ file! = "." & $ File! = ".."){
  14. $ Exit [] = $ file ;}
  15. }
  16. Closedir ($ dh );
  17. }
  18. }
  19. // Obtain the file image address in the database
  20. $ SQL = "select $ field from $ table ";
  21. $ Query = mysql_query ($ SQL );
  22. While ($ row = mysql_fetch_array ($ query )){
  23. $ Pic [] = $ row [pic];
  24. }
  25. // The function here is very important. array_diff_key seems to be a problem.
  26. $ Result = @ array_diff ($ exit, $ pic );
  27. If ($ result = null ){
  28. Echo "you have no files to delete ";
  29. }
  30. Foreach ($ result as $ id => $ values ){
  31. Echo "the file deleted by the system is". $ values ."
    ";
  32. $ File = $ dir. $ values;
  33. Unlink ("$ file ");
  34. }
  35. }
  36. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.