Search code examples
phpregexpreg-matchglobunlink

PHP Unlink to remove old images via regex


I'm trying to make a php script that will search a specific path in my website and remove a series of images that are old and unused.

They can be ending in either .jpg, .jpeg, .gif, .png and some also have .webp appended onto those that I have just mentioned.

The format of the images usually ends in: -xxxx-xxxx-.extension for example:

  • 32x32.png.webp
  • 50x150.png
  • 720x1024.gif
  • 1920x1920.jpeg.webp

The following regex detects them all correctly as far as I can see:

  • ^(.+[0-9]+.[0-9]+\.(png|gif|jpe?g)\.?(webp)?)$

I've tried the following, but for some reason it's not doing anything:

<?php
    $glob = glob('/home/HOSTACC/public_html/fx/upd/*');
    foreach($glob as $file) {
        if(preg_match('^(.+[0-9]+.[0-9]+\.(png|gif|jpe?g)\.?(webp)?)$', $file)) {
            unlink($file);
        }
    }
?>

My directory /home/HOSTACC/public_html/fx/upd/ contains multiple folders with images inside, some examples below:

  • /home/HOSTACC/public_html/fx/upd/y-icon-sprite-213x300.png
  • /home/HOSTACC/public_html/fx/upd/01/desx-yopy-small-150x150.png
  • /home/HOSTACC/public_html/fx/upd/old/maximumload-web-1920x1920.png
  • /home/HOSTACC/public_html/fx/upd/old/maximumload-web-1920x1920.png.webp
  • /home/HOSTACC/public_html/fx/upd/ex/new/old-storage-720x500.png

Recursive:

<?php
    echo "<pre>";

    function rsearch($folder, $pattern_array) {
        $return = array();
        $iti = new RecursiveDirectoryIterator($folder);
        foreach(new RecursiveIteratorIterator($iti) as $file){
            if (in_array(strtolower(array_pop(explode('.', $file))), $pattern_array)){
                $return[] = $file;
            }
        }
        return $return;
    }
    $filepaths = rsearch('/home/HOSTACC/public_html/fx/upd/', array('jpeg', 'jpg', 'jpeg', 'png', 'gif', 'webp') );

    foreach($filepaths as $file) {
        /* The following line prints the correct paths*/
        $filename = $file->getFilename();
        $filepath = $file->__toString();
        $filenamenew = end((explode('/', $filepath)));
        echo var_dump($filename);
        echo var_dump($filenamenew);
        echo var_dump($filepath);
        if(preg_match('/^(.+[0-9]+.[0-9]+\.(png|gif|jpe?g)\.?(webp)?)$/‌​', $filename)) {
            /*unlink($file);*/
            echo "FOUND:" . $filepath . PHP_EOL;
        }
        echo PHP_EOL;
    }
?>

Output (Short:

string(21) "videos_background.png"
string(21) "videos_background.png"
string(70) "/home/HOSTACCO/public_html/f1/docs/uploads/static/videos_background.png"

string(29) "videos_background-150x150.png"
string(29) "videos_background-150x150.png"
string(78) "/home/HOSTACCO/public_html/f1/docs/uploads/static/videos_background-150x150.png"

The only issue is that the preg_match isn't working, not sure what I've done wrong: https://regex101.com/r/EMrI0N/1


Solution

  • You seem to have a non-printing control character embedded in your regular expression. Copying and pasting your code from here to 3v4l.org shows:

    Warning: preg_match(): Unknown modifier '�' in /in/pXQUE on line 4
    

    I've manually rekeyed it and now it works:

    FOUND:videos_background-150x150.png
    

    I suggest either rekeying in your source, or copying mine from 3v4l.