Clearing the Files in a Folder

Hi There

I have this code that i use to upload images to a folder that then are displayed on a page, but theres only four images, and if i update them i want the previous ones to dissapear, so i would like to know.

how am i going to delete the folder or clear it and then again make the folder so that the new images can go in it.

i’ve tried using rmdir and then mkdir again, but that isn’t working either.

i even tried using a function that delete’s the folder and then i tried using the mkdir right after the function, but then its like it doesn’t wanna do it, it just delete’s the folder until i refresh and run the code again, then it makes the dir.

any advice of how i can go about doing this?

this is my code i use uploading the images :


<?php
 // Connects to your Database 
 include 'includes/connection.php';
 
 //This is the directory where images will be saved
 $target1 = "../advert_middle/"; 
 $target1 = $target1 . basename($_FILES['ad1']['name']); 
 $target2 = "../advert_middle/"; 
 $target2 = $target2 . basename($_FILES['ad2']['name']); 
 $target3 = "../advert_middle/"; 
 $target3 = $target3 . basename($_FILES['ad3']['name']); 
 $target4 = "../advert_middle/"; 
 $target4 = $target4 . basename($_FILES['ad4']['name']); 

 //This gets all the other information from the form 
 $ad1=($_FILES['ad1']['name']); 
 $ad2=($_FILES['ad2']['name']); 
 $ad3=($_FILES['ad3']['name']); 
 $ad4=($_FILES['ad4']['name']); 
 
 //Writes the information to the database 
 $query = "UPDATE adm_advert SET filename = '$ad1', filename1 = '$ad2', filename2 = '$ad3', filename3 = '$ad4' ";
 mysql_query($query) or die('Error ,query failed'); 
 
 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['ad1']['tmp_name'], $target1) & move_uploaded_file($_FILES['ad2']['tmp_name'], $target2) & move_uploaded_file($_FILES['ad3']['tmp_name'], $target3) & move_uploaded_file($_FILES['ad4']['tmp_name'], $target4)) 
 { 
 //Tells you if its all ok 
 header("location: staff.php?page=home_advert_success"); 
 } 
 else { 
 
 //Gives and error if its not 
 echo header("location: staff.php?page=home_advert_fail");
 } 
 ?>

any help please…

i use this function to delete directories if it helps you:


function delete_directory($dirname_dele) {
   if (is_dir($dirname_dele))
      $dir_handle_dele = opendir($dirname_dele);
   if (!$dir_handle_dele)
      return false;
   while($file_dele = readdir($dir_handle_dele)) {
      if ($file_dele != "." && $file_dele != "..") {
         if (!is_dir($dirname_dele."/".$file_dele))
            unlink($dirname_dele."/".$file_dele);
         else
            delete_directory($dirname_dele.'/'.$file_dele);    
      }
   }
   closedir($dir_handle_dele);
   rmdir($dirname_dele);
   return true;
}

An idea which ive done in the past is that i just ran a loop of all the files in the directory deleting those which do not match files found in the database. Do a select first and then just match them using simple nested if statements:


$dir_icon_tmp = opendir($dirimgicon."/");
//List files in images directory
while (($file_icon_tmp = readdir($dir_icon_tmp)) !== false)
{
// check against valid filenames
if ($file_icon_tmp != $ad1) {
if ($file_icon_tmp != $ad2) {
if ($file_icon_tmp != $ad3) {
if ($file_icon_tmp != $ad4) {
$dele = $file_icon_tmp;
if (is_file($dele)) {
//If the file dosn't belong. Delete it
unlink($dele);
}
}
}
}
}
}

Obviously the code above would need tweaking to ensure the vars go to your correct directory structure. But as there is only ever going to be 4 images in that directory then it should be not much of a strain to run the code.

Okay i added this :


 $dirimgicon = "../advert_middle";
 
 $select = "SELECT filename, filename1, filename2, filename3 FROM adm_advert";
 mysql_query($select) or die('Error ,query failed'); 
 
$dir_icon_tmp = opendir($dirimgicon."/");
//List files in images directory
while (($file_icon_tmp = readdir($dir_icon_tmp)) !== false)
{
// check against valid filenames
if ($file_icon_tmp != $filename) {
if ($file_icon_tmp != $filename1) {
if ($file_icon_tmp != $filename2) {
if ($file_icon_tmp != $filename3) {
$dele = $file_icon_tmp;
if (is_file($dele)) {
//If the file dosn't belong. Delete it
unlink($dele);
}
}
}
}
}
}

and it runs, but it doesn’t delete the files still in the folder, so as i go on uploading new files everytime to replace the old ones it just keeps stacking up in the folder. am i doing something wrong?