2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Sorcher
0

Hey,

How about trying something like this:

$excludeExtensions = array('jpg', 'gif', 'png');
$directory = '/path/to/directory';
$files = scandir($directory);

foreach($files as $file) {
    if(!in_array(pathinfo($file, PATHINFO_EXTENSION), $excludeExtensions))) {
        unlink("{$directory}/{$file}");
    }
}

This code will iterate through all of the files found in a target directory. It will then delete any files with an extension that is not in the exclude array.

Hope this helps.
R.

0

Hey,

How about trying something like this:

$excludeExtensions = array('jpg', 'gif', 'png');
$directory = '/path/to/directory';
$files = scandir($directory);

foreach($files as $file) {
    if(!in_array(pathinfo($file, PATHINFO_EXTENSION), $excludeExtensions))) {
        unlink("{$directory}/{$file}");
    }
}

This code will iterate through all of the files found in a target directory. It will then delete any files with an extension that is not in the exclude array.

Hope this helps.
R.

Thanks allot ! Worked fine :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.