Welcome Guest, Not a member yet? Register   Sign In
Session files
#2

(This post was last modified: 11-18-2024, 11:33 PM by InsiteFX.)

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 )
Reply


Messages In This Thread
Session files - by paulkd - 11-16-2024, 01:20 AM
RE: Session files - by InsiteFX - 11-18-2024, 11:30 PM
RE: Session files - by paulkd - 11-19-2024, 08:21 AM
RE: Session files - by InsiteFX - 11-19-2024, 11:14 PM



Theme © iAndrew 2016 - Forum software by © MyBB