Here are 2 helper method's that I wrote to clear out session and log files in the writable folder.
Do not use on a live sever, for a live server please use a corn job to delete them.
PHP Code:
/**
* clearSessionFiles ()
* -----------------------------------------------------------------------
*/
if ( ! function_exists('clearSessionFiles'))
{
/**
* clearSessionFiles ()
* -------------------------------------------------------------------
*
*/
function clearSessionFiles(): void
{
// Counter for number of session files - dir . & .. and index.html
$count = 0;
// Get all files in our session folder
$listFile = scandir("../writable/session/");
foreach ($listFile as $file) {
// if the file is a . or .. directory skip it!
if ( ! is_dir("../writable/session/" . $file)) {
$count++;
}
// We now have all session files and will exclude the index.html file
if (! is_dir("../writable/session/" . $file)) {
if ($file !== "index.html") {
// for debugging count of files
//echo $count . " " . $file . "<br>";
// unlink and delete the files
unlink("../writable/session/" . $file);
}
}
}
// Close the session, it will re-intialize itself.
session()->close();
// Log message
log_message('debug', "clearSessionFiles() - Deleted " . $count . " Session files");
}
}
/**
* clearLogFiles ()
* -----------------------------------------------------------------------
*/
if ( ! function_exists('clearLogFiles'))
{
/**
* clearLogFiles ()
* -------------------------------------------------------------------
*
*/
function clearLogFiles(): void
{
// Counter for number of log files - dir . & .. and index.html
$count = 0;
// Get all files in our logs folder
$listFile = scandir("../writable/logs/");
foreach ($listFile as $file) {
// if the file is a . or .. directory skip it!
if ( ! is_dir("../writable/logs/" . $file)) {
$count++;
}
// We now have all log files and will exclude the index.html file
if (!is_dir("../writable/logs/" . $file)) {
if ($file !== "index.html") {
// for debugging count of files
//echo $count . " " . $file . "<br>";
// unlink and delete the log files
unlink("../writable/logs/" . $file);
}
}
}
// Log message
log_message('debug', "clearLogFiles() - Deleted " . $count . " Log files");
}
}
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )