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.
- // Created on 2010-4-24 Made by Wolf QQ116311316
- // Function: determines whether the fields in the database correspond to the files in the folder.
- // Delete the file, greatly reducing the usage of system resources.
- // $ Dir is the Directory of the file to be searched
- // $ Table: The table to be searched
- // $ Field is the field name
- // ==================================
- Function delfile ($ dir = "", $ table, $ field ){
- // Obtain files in the directory
- If (is_dir ($ dir )){
- If ($ dh = opendir ($ dir )){
- While ($ file = readdir ($ dh ))! = False ){
- If ($ file! = "." & $ File! = ".."){
- $ Exit [] = $ file ;}
- }
- Closedir ($ dh );
- }
- }
- // Obtain the file image address in the database
- $ SQL = "select $ field from $ table ";
- $ Query = mysql_query ($ SQL );
- While ($ row = mysql_fetch_array ($ query )){
- $ Pic [] = $ row [pic];
- }
- // The function here is very important. array_diff_key seems to be a problem.
- $ Result = @ array_diff ($ exit, $ pic );
- If ($ result = null ){
- Echo "you have no files to delete ";
- }
- Foreach ($ result as $ id => $ values ){
- Echo "the file deleted by the system is". $ values ."
";
- $ File = $ dir. $ values;
- Unlink ("$ file ");
- }
- }
- ?>
|