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

Detail Expenses Report by Cost Center
I would like to obtain a report out of SAP that shows a list of expenses by cost center that shows t

TemplatePower & AJAX
Hi all,



I'm currently implementing some AJAX features in my PHP-framework (which is b

Generate multilayered array from string.
ok so i have a string that looks like this:
Code: blog:edit_all,delete_all|users:edit_all,delete_

Problem writing URL into database
How would I write this into the database?

<?php echo "http://".$_SERVER['SER

matching numbers inside ( )
I know I can match numbers by just [0-9]+, so I thought matching numbers inside ( ) would be somethi

Accessing Infotype data in dialog program
Hi All
In Dialog programs attributes I didn't see any logical database field. How can I access

Troubles with a spider class
I am building a spider that will crawl through random whitepages (eg. anywho.com, switchboard.com, w

Standard DES encryption script?
Can anybody point me to a tutorial or a short script that shows how to encrypt some text with salt i

Multiple arrays inside data
Hi,

Simple question. I have a column called "array" in my database, and inserted in

Help: problem with Headers to download PDF file
Hi,

I'm trying to implement a script where when a user clicks a link he gets a PDF file to op

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