Delete files based on file records in the database

WBOY
Release: 2016-07-25 09:11:32
Original
1124 people have browsed it
Determine whether the fields in the database correspond to the files in the folder. If there is no correspondence, delete the files, which greatly reduces the occupation of system resources.
  1. //Created on 2010-4-24 Made by Wolf QQ116311316
  2. //Function: It is to determine whether the fields in the database correspond to the files in the folder. If there is no correspondence
  3. //then Deleting files greatly reduces system resource usage.
  4. //$dir is the file directory to be searched
  5. //$table is the table to be searched
  6. //$field is the field name
  7. //================ ====
  8. function delfile($dir = "",$table,$field){
  9. //Get the 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. //Get 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 was originally used. There seems to be something wrong
  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. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!