Results 1 to 6 of 6

Thread: Delete all images from filesystem folder EXCEPT a,b,c

  1. #1
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Delete all images from filesystem folder EXCEPT a,b,c

    Hi, i'm trying to delete all images in a folder except three known ones (whos full URI's are drawn from a table and held by $logo,$picture and $banner) via a php script in templates.
    $banner for instance will give me "tl_files/SchoolMembers/eeee/auckland-university-clock-tower.jpg"
    $membersfolderpath will give me "tl_files/SchoolMembers/eeee/"

    I can delete $banner like this:
    Code:
    $removefiles = new File($banner);
    $removefiles->delete();
    I'm hoping someone can help with the code to delete all images present in the folder tl_files/SchoolMembers/eeee/ (held in $membersfolderpath) EXCEPT for the images held by $logo,$picture and $banner.

  2. #2
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Delete all images from filesystem folder EXCEPT a,b,c

    Quote Originally Posted by ramjet
    Hi, i'm trying to delete all images in a folder except three known ones (whos full URI's are drawn from a table and held by $logo,$picture and $banner) via a php script in templates.
    $banner for instance will give me "tl_files/SchoolMembers/eeee/auckland-university-clock-tower.jpg"
    $membersfolderpath will give me "tl_files/SchoolMembers/eeee/"

    I can delete $banner like this:
    Code:
    $removefiles = new File($banner);
    $removefiles->delete();
    I'm hoping someone can help with the code to delete all images present in the folder tl_files/SchoolMembers/eeee/ (held in $membersfolderpath) EXCEPT for the images held by $logo,$picture and $banner.
    take a look at "scan" function http://dev.typolight.org/browser/tru...tions.php#L201

    you can do something with array_diff (http://php.net/array_diff)
    Consulenza Contao CMS https://www.intco.it

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Delete all images from filesystem folder EXCEPT a,b,c

    scan() returns an array of a.jpg,b.gif,c.jpg etc.
    Any idea what i would use so that $membersfilepath (tl_files/SchoolMembers/eeee/) is prepended to each item in the array?

  4. #4
    User
    Join Date
    06-19-09.
    Location
    Kosice, Slovakia
    Posts
    61

    Default Re: Delete all images from filesystem folder EXCEPT a,b,c

    Using foreach is easiest IMHO.

    Code:
    $files = scan(...);
    foreach ($files as $file)
    {
      $fileWithPath = $membersfolderpath . $file;
      if ($fileWithPath != $logo && $fileWithPath != $picture && $fileWithPath != $banner)
      {
        // delete it 
        $removefiles = new File($fileWithPath);
        $removefiles->delete();
      }
    }
    S.C.A.R.E

  5. #5
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Delete all images from filesystem folder EXCEPT a,b,c

    Thanks guys,much appreciated.... I went about it slightly differently using array_walk().
    I don't know if there are any advantages/disadvantages between my way and yours scare? (but yours certainly looks more elegant :D )
    Code:
    $membersfolderpath = "tl_files/SchoolMembers/" . $username . "/";
    
    //make array of banner,picture,logo (already contains full path)
    $imagesarray = array($banner,$picture,$logo);
    
    //get all files in members folder
    $scanfiles = scan($membersfolderpath);
    
    //prepend $membersfolderpath to each element in the $scanfiles array to give full path
    function alterScanfiles(&$item1, $key, $prefix)
    {
        $item1 = "$prefix$item1";
    }
    array_walk($scanfiles, 'alterScanfiles', $membersfolderpath);
    
    //make array of the difference between $scanfiles and $imagesarray
    $difference = array_diff($scanfiles,$imagesarray);
    
    
    //LOOP thru $difference array and delete files
    foreach ($difference as $key => $value) {
    $removefiles = new File($value);
    $removefiles->delete();
    }

  6. #6
    User
    Join Date
    06-19-09.
    Location
    Kosice, Slovakia
    Posts
    61

    Default Re: Delete all images from filesystem folder EXCEPT a,b,c

    Well, there may be problem if you include your code on some page twice - alterScanfiles function would be defined twice which generates an error. You mentioned the code resides in some template, so make sure that you don't place two modules using the template on one page. Or include your function definition in if (!function_exists('alterScanfiles')) {} block. Otherwise no real difference there ;-)
    S.C.A.R.E

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
  •