Jump to content

[HOW] Auto Delete a Folder content by Cron Job after no of days!!!


natasha_thomas

Recommended Posts

Friends,

 

I want a way/script/code to auto delete a folder content (Folder name is "Gallary") I mean i want to Empty the Folder "GAllary", in each X no of days.

 

How can i achieve this, any cron Job Script or Code?

 

May someone help?

 

(I am on hostgator Sharedhosting... So i think its Unix Server with Cpanel)

I have dived into Linux permissions and to remove a file/directory you need the write permission. The below script should do the job and report if something isn't correct.

 

$directoryPath = realpath('path/to/directory');
if (!is_dir($directoryPath)) {
  error_log("Directory $directoryPath does not exist.");
  exit(0);
}

function cleanDirectory($directoryPath, $recursive = FALSE)
{
  if (!is_readable($directoryPath) || !is_writable($directoryPath)) {
    error_log("Can't delete directory $directoryPath as you don't have the correct permissions.");
    return FALSE;
  }
  
  $folderEmpty = TRUE;
  foreach (scandir($directoryPath) as $file)
  {
    $filePath = realpath("$directoryPath/$file");
    if (is_dir($filePath)) {
      if (!cleanDirectory($filePath, $recursive)) {
        $folderEmpty = FALSE;
      }
      continue;
    }
    
    if (!deleteFile($filePath)) {
      $folderEmpty = FALSE;
    }
  }
  
  return $folderEmpty ? @rmdir($directoryPath) : FALSE;
}

function deleteFile($filePath) {
  if (!is_writable($filePath)) {
    error_log("Can't delete file $filePath as you don't have the correct permissions.");
    return FALSE;
  }
  return @unlink($filePath);
}

I have dived into Linux permissions and to remove a file/directory you need the write permission. The below script should do the job and report if something isn't correct.

 

$directoryPath = realpath('path/to/directory');
if (!is_dir($directoryPath)) {
  error_log("Directory $directoryPath does not exist.");
  exit(0);
}

function cleanDirectory($directoryPath, $recursive = FALSE)
{
  if (!is_readable($directoryPath) || !is_writable($directoryPath)) {
    error_log("Can't delete directory $directoryPath as you don't have the correct permissions.");
    return FALSE;
  }
  
  $folderEmpty = TRUE;
  foreach (scandir($directoryPath) as $file)
  {
    $filePath = realpath("$directoryPath/$file");
    if (is_dir($filePath)) {
      if (!cleanDirectory($filePath, $recursive)) {
        $folderEmpty = FALSE;
      }
      continue;
    }
    
    if (!deleteFile($filePath)) {
      $folderEmpty = FALSE;
    }
  }
  
  return $folderEmpty ? @rmdir($directoryPath) : FALSE;
}

function deleteFile($filePath) {
  if (!is_writable($filePath)) {
    error_log("Can't delete file $filePath as you don't have the correct permissions.");
    return FALSE;
  }
  return @unlink($filePath);
}

 

Thanks IGnace...

 

Where can i set the Time i mean seconds or no of days after while the Folder should be emptied automatically....?

this is how you setup a cron with hostgator - http://support.hostgator.com/articles/cpanel/how-do-i-create-and-delete-a-cron-job

 

and here is a php snippet that empties a directory- http://snippets.dzone.com/posts/show/5004

 

Thank you!!! I appreciate!!!

 

was curious to know, Where can i set the Time i mean seconds or no of days after while the Folder should be emptied automatically....?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.