Anyone know a snippet of this?
I need to delete all files in a folder but not jpg,gif or png files.
:sweat:

Recommended Answers

All 2 Replies

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.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.