Jump to content

Delete all files in a folder except one with a certain name


jamiet757

Recommended Posts

I am trying to make a script that I can run via cron jobs that will delete the main image file and 1 thumb, but leaving a smaller thumb from images that have been declined on my site. Here is the code I have so far:

 

function delete_old_declined_files($id)//need to figure out timing and also how to delete everything but thumb1.jpg
{
global $db;
$dp = new TMySQLQuery;
$dp->connection = $db;

$folder="";

$sql="select folder from photos where published=2 and data>";//<---SOME TIME CODE TO SELECT PHOTOS OLDER THAN 7 DAYS
// possibly this: mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y")) with some math functions
//SELECT DECLINED FILES
$dp->open($sql);
if(!$dp->eof)
{
$folder=$dp->row["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);
}

}

//End Function
//Delete Old Declined Files

$sql="select id_parent,userid,folder from photos where (userid=".(int)$_SESSION["people_id"]." or author='".result($_SESSION["people_login"])."') and id_parent=".(int)$_GET["id"];
$rs->open($sql);
if(!$rs->eof)
{

delete_old_declined_files((int)$_GET["id"]);

}

 

 

I need to figure out how to make it only get photos that are older than 7 days, but I cannot figure out the correct context to select folder from photos if it is older than 7 days.

 

Also, I need to figure out how to make it delete 1 file with a random name, 1 file named thumb2.jpg, but leave thumb1.jpg and the folder intact. I know the code is already there, but I can't figure out which sections to get rid of to accomplish my goal.

Link to comment
Share on other sites

I've read your post a few times and it makes less sense each time I read it. The title states you want to delete all fiels in a folder except one with a certain name. Then the first paragraph you talk about deleting "declined" photos but provide no explanation of how that is determined. Then in the second paragraph you talk about deleting only files older than 7 days. And, in the last paragraph you state you need to delete files based on their names.

 

What - EXACTLY - are you trying to accomplish?

Link to comment
Share on other sites

Sorry for the unclear explanation, when I say declined, that is referring to the way the files are marked in the database, "where published=2" means declined.

 

I want to delete the files in the specified folder (taken from the database where published=2 and it is older than 7 days) but leave 1 file (thumb1.jpg) and the folder intact. There are 2 other files in each folder, thumb2.jpg, and another .jpg that could have any name. I want to delete those two files (which I think it would be easier to just delete all files EXCEPT thumb1.jpg) when the date (in the DATA column) is older than 7 days.

 

I hope that makes a bit more sense.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.