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?

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

what business processes?
Hi,

A general question. What business processes (like OTC - Order to Cach, P2P, R2R etc)

Passing variables with pagination - iterating through unique id per link
Hi everyone,
I have seen a few topics like this one. Still cant find the specific thing, so I am

Adding Different Numbers From MySQL
Basically I have a mysql database with a couple prices as the following:

9.99
9.99
9.99

updating a single value to multiple non consecutive rows
I need to update the same value to multiple non consecutive rows. I need to add an experation date t

Adding to the next element in a multidimensional array
Hi, I'm trying to add a value to $node->field_spaces['nid'] where x is the next available spo

close site for maintenance
i get a tutorial, saying the following code can put our site offline, and only the developer can vie

Calling All PHPFREAKS Blackberry Users
Are you a member of PHPFreaks and have a blackberry? Well RIM just recently released Blackberry Mes

Backflush
I am looking for a report in SAP that would give me a list of all parts that are populated with a ba

CURL XML Request [From C to PHP]
Hi,

I need some help converting this into something that will work with PHP.

Code: #

Cache PHP Objects/Classes?
Does anyone have ideas about caching PHP objects using something like: http://memcached.org/

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