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

captcha error
I have been trying to implement a captcha in php...

here is the code..
Code: (php) [Select

upload photo limits
ive got a upload photo script and im just trying to make more secure currently it limits size and as

Prevent PHP mail( ) from appending hostname to from address
Hey guys,

I want to be able to send texts to phones via email (since each phone has their own

PHP/PKI
I am trying to set up a web application that uses pki. does anyone have a good tutorial to set this

Using two $_POST Function / Switch () statements, second does not work.
Hi all. I’m new to php and am having a problem getting $_POST Function / switch () to work. I

How to calculate days from variable date?
This will be easy for one of you gurus. I want to fetch the date from a variable date, for example:<

Expandable Post Code
Hi,

Hope you can help. Which currently I haven't upgraded to connect to a mysql server, so i

While Problem
i am having a problem with a while statement here is the code
Code: [Select]<?php
sess

I would like "for" to wait 5 sec
I would like my 'for' loop to wait 5 sec before second iteration.


for ($i=0; $i<10

Something like an INI editor or a DelimitedText-Editor
Hi all,

Am very, very, very new to PHP and not sure if I should be posting this to a Javascri

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