Hey,

I am building a classifieds section on my site and need some help with the following.

Listings are added for 6 months. After this time the listing is not shown on the site but the information is stored for another 7 days incase they want to extend their listing. After this time the listing is deleted from the site.

Heres my problem.

I need to automatically delete the old listing and the images. I can delete the listing and the image from the database, but cant delete the folder that contains the images. This is what i tried:

$result = mysql_query("SELECT i_id FROM classified_other WHERE DATE_ADD(i_expires, INTERVAL 7 DAY) < NOW()") 
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$dir = "images/classified/other/".$row['i_id']."";
$id = $row['i_id'];
function rmdirr($dir) {
   if($objs = glob($dir."/*")){
       foreach($objs as $obj) {
           is_dir($obj)? rmdirr($obj) : unlink($obj);
       }
   }
   rmdir($dir);
}

$delete_img = "DELETE FROM classified_img WHERE i_id = $id";
mysql_query($delete_img, $tidp_main) or die (mysql_error());

$delete_listing = "DELETE FROM classified_other WHERE i_id = $id";
mysql_query($delete_listing, $tidp_main) or die (mysql_error());
} 

The folder is created when they upload an image and the folder name is the listing id. So listing id 10 will be here: images/classified/other/10

also inside this folder a thumbnail folder is created which also needs deleting:

images/classified/other/10
images/classified/other/10/thumbnails

Any help would be great!

Thanks,
Danny Lewin

    You need to have permission to delete dir ...

    Do u chmod your dir when u create them?

    And create a recursive function to parse all the dirs, if the main dir u want to delete have subdirs ...

      <?php

      function remove_directory($dir) {
      if ($handle = opendir("$dir")) {
      while (false !== ($item = readdir($handle))) {
      if ($item != "." && $item != "..") {
      if (is_dir("$dir/$item")) {
      remove_directory("$dir/$item");
      } else {
      unlink("$dir/$item");
      echo " removing $dir/$item<br>\n";
      }
      }
      }
      closedir($handle);
      rmdir($dir);
      echo "removing $dir<br>\n";
      }
      }

      remove_directory("/path/to/dir");

      ?>

        sorry that was a quick reply here's the indented version:

        <?php
        
        function remove_directory($dir) {
          if ($handle = opendir("$dir")) {
           while (false !== ($item = readdir($handle))) {
             if ($item != "." && $item != "..") {
               if (is_dir("$dir/$item")) {
                 remove_directory("$dir/$item");
               } else {
                 unlink("$dir/$item");
                 echo " removing $dir/$item<br>\n";
               }
             }
           }
           closedir($handle);
           rmdir($dir);
           echo "removing $dir<br>\n";
          }
        }
        
        remove_directory("/path/to/dir");
        
        ?> 
          kidgeek_dfw wrote:

          sorry that was a quick reply here's the indented version:

          That's why posts come with an Edit button.....

            Write a Reply...