Jump to content

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....?

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.