Delete all files in folder except with certain name


Posted on 16th Feb 2014 07:03 pm by admin

I have a script that I want to delete all files in a folder (taken from a database) except for one named thumb1.jpg

I have this, but this deletes all the files and a subfolder if there is one. How can I modify it to leave the folder and thumb1.jpg intact, but remove any other files in this folder?

Did you know?Explore Trending and Topic pages for more stories like this.
if($folder!="" and file_exists($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder))
{
$dir = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder);
while ($file = readdir ($dir))
{
if($file <> "." && $file <> "..")
{
if(is_dir($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file))
{
$dir2 = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file);
while ($file2 = readdir ($dir2))
{
if($file2 <> "." && $file2 <> "..")
{
@unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file."/".$file2);
}
}
@rmdir($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file);
}
else
{
@unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file);
}
}
}
@rmdir($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder);
}

}
No comments posted yet

Your Answer:

Login to answer
289 Like 48 Dislike
Previous forums Next forums
Other forums

Converting RGB values to HEX
Code: <?PHP

$file_handle = fopen("colors/rgb.csv", "r");

wh

ScriptManager History and Opera Issue
I am having an issue in opera with the ScriptManager. I have a ScriptManager on a page with history

eregi to preg
Im converting my regex for php 5.3 and I am stuck on the following

Code: [Select]if((eregi(&q

SWF image using php?
Hello once again.

My latest en devour requires me to produce an image of a static .swf that i

Take info from one coloum and move to another
Hi all,

I have this:
Code: [Select]$array = "SELECT stock_id FROM stocks WHERE stock_

My XSRF Prevention code isn't working
First of all, thanks for the generous help you guys have given me in the past on this forum.
Seco

Significance of BPM
Hi Experts,
I am a novice in BPM , I just want to know how BPM as permenant department is

Javascript form submit and radio buttons?
When this form is submitted, it is automatically resubmitted using JS.

All my fields are carr

Problem with umlauts and UTF-8
Hello every1,

I'm creating my own little CMS at the moment and got some problems with the cha

serializing objects - loses methods - the point being?
Hi

I know that serializing an object will lose the methods. But whats the point in that? What

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash