SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question getting all file in a subdirectory for comparison and unlink after that....

    hi, i have a main directory where sub directory will be created according to user ID when the user choose to upload photo...

    in the database, i only save the photo name(eg."123.jpg")

    now, i wish to do a directory clean up.

    i want to get all the files in directory listed as main_directory/subdirectory/imagename.jpg

    then i'll compare what i have in the database...

    if the image file in the subdirectory not occur in the database record, it will unlink it..

    i've do something similar before this:


    PHP Code:
    $query="SELECT photo FROM userfile";
    $result=mysql_query($query)or die(mysql_error());

    $dbfiles=array();
    while (
    $row=mysql_fetch_array($result))
    {
        
    $dbfiles[]=$row['photo'];
        }

        echo 
    "<br><br>";

        
    $dir "uploaded_images";
        
    $dirfile=array();
    if (
    is_dir($dir)) { 
       if (
    $dh opendir($dir)) { 
           while ((
    $file readdir($dh)) !== false) { 
               if (
    $file != '.' && $file != '..')
                
    $dirfile[] = $file;
           } 
           
    closedir($dh); 
       } 


        
    $delete array_diff($dirfile,$dbfiles); 
        
    var_dump($delete);

        foreach (
    $delete as $delete_file) {
        echo 
    $delete_file;
       
    $deletefile="uploaded_images/".$delete_file;
       
    unlink($deletefile);


    how can i modify the above code to suit my current condition???

    thanks!

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script needs rewritten.
    Main points:
    1. You need the user id to get the image path like 'uploaded_images'.$id_user;
    2. First retrieve all the file names(and user ids) form the db in an array like $dbfile[$id][$name].
    3. Then for each $id in $dbfile:
    • $dir=uploaded_images/'.$id.'/';
    • for each $file in $dir if not isset($dbfile[$id][$file]) then unlink($dir.$file);

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    praetor,

    thanks for your guidance...

    i'll try on it now!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •