1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Delete all files that AREN'T a certain extension?

Discussion in 'PHP' started by dp-user-1, Sep 9, 2008.

  1. #1
    Hello,

    I've been trying to find an elegant way to delete all files in a directory that aren't .jpg files. I also need to exclude a single file (let's call it keep.php) from deletion.

    I've been looking into the glob function, however the best I can come up with is how to delete ONLY .jpg files.

    I'm sure it's an easy fix... any ides?

    Thanks,
    Peter
     
    dp-user-1, Sep 9, 2008 IP
  2. wmcoder

    wmcoder Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    glob should work for this or opendir
    can you just loop through the files and specify which ones to delete or omit from deletion?
     
    wmcoder, Sep 9, 2008 IP
  3. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Hi Here is the Solution
    <?php
    $dir = "./";
    
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
               	$array_name=explode(".",$file);
    			$extension=$array_name[1];
    
    			if(strtolower(trim($extension))=="jpg")
    			{
    				unlink($dir.$file);
    				print "Deleted > ".$file."<br>";
    			}
    
            }
            closedir($dh);
        }
    }
    ?>
    
    PHP:
    Thanks,
     
    nice.wallpapers, Sep 9, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    They want to delete all files that AREN'T a jpg file so you'd have to modify your if statement to be !="jpg" rather than =="jpg"
     
    zerxer, Sep 10, 2008 IP
  5. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #5
    yes :)

    Thanks,
     
    nice.wallpapers, Sep 10, 2008 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    So I guess glob() isn't the solution here? Is it possible to accomplish this using glob()?

    Thanks for the help.

    -Peter
     
    dp-user-1, Sep 10, 2008 IP
  7. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Hi,

    It can also be done by glob function
    here is the code :)
    <?php
    foreach (glob("*.*") as $filename) {
    			$array_name=explode(".",$filename); 
    			$extension=$array_name[1];  
    
    			if(strtolower(trim($extension))!="jpg")
    			{
    				unlink($filename);
    				print "Deleted > ".$filename."<br>";
    			}
    }
    ?>
    PHP:
    Thanks,
     
    nice.wallpapers, Sep 10, 2008 IP
    NinjaNoodles likes this.
  8. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    One thing about your code.. doing "$extension=$array_name[1];" isn't a good idea because what if the filename has periods in it?

    $extension= substr($filename, strrpos($filename, ".")+1);
    PHP:
     
    zerxer, Sep 11, 2008 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    This should skip all capitalization and extra periods in file:

    
    <?php
    $file_list = glob("*.*");
    foreach($file_list as $file_name){
           $extension = strtolower(end(explode('.',$file_name)));
           if($extension != 'jpg' && $extension != 'jpeg'){
                 unlink($file_name);
           }
         
    }
    
    PHP:

    Peace,
     
    Barti1987, Sep 11, 2008 IP
  10. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That should work, azizny, except I think you meant to use the && operator, not the || operator. With that method, it'll delete every file including jpgs and jpegs.
     
    zerxer, Sep 11, 2008 IP
    Barti1987 and NinjaNoodles like this.
  11. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #11
    +rep, fixed.

    Peace,
     
    Barti1987, Sep 11, 2008 IP
    NinjaNoodles likes this.
  12. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #12
    Great. :)

    One of the problems I was running into was files with periods in the names, and I appreciate the help.

    Thanks a lot guys, rep added.

    -Peter
     
    dp-user-1, Sep 11, 2008 IP