> 1000 files to unlink.
$careful = 20;
$GLOBALS['arg1'] = "/tmp/";
$GLOBALS['arg2'] = "sess_*";
$GLOBALS['arg3'] = "";
function existfileinfolder($arg1="",$arg2="",$arg3="") {
$folder = $arg1;
$fname = $arg2;
$ext = $arg3;
// Default to folder /tmp/
// (remove complete 'if statement' if use for other folder)
if ($folder != "/tmp/") {
$folder = "/tmp/";
}
// Only remove above IF YOU KNOW WHAT YOU ARE DOING.
foreach (glob($folder.$fname.$ext) as $filename) {
$delme[] = $filename;
}
return $delme;
}
function check() {
// Take in the globals in this function.
$arg1 = $GLOBALS['arg1'];
$arg2 = $GLOBALS['arg2'];
$arg3 = $GLOBALS['arg3'];
// Make/set the array $delme global, reachable outside this function.
$GLOBALS['delme'] = existfileinfolder($arg1,$arg2,$arg3);
// Make/set the variable $totals global, reachable outside this function.
$GLOBALS['totals'] = (int)count($GLOBALS['delme']);
$breaknowat = $GLOBALS['totals'];
if ((int)$breaknowat >= 1) {
return TRUE;
} else {
return FALSE;
}
}
while (check() == TRUE) {
echo("I'm now trying to delete file : ".$delme[0]." of total now ".$totals."
");
// Try to only delete the first value in array, nameley delme[0],
// the array contents shifts after each while loop if success to delete.
// Do not produce any PHP error messages, use @unlink().
if (@unlink($delme[0])) {
echo($delme[0]." was deleted..!
");
$n++;
} else {
echo("Check permission on folder/file ".$delme[0]."
");
$halt = 1;
break;
}
// Careful counter; $i++ and a break if overrun. But if it overruns! There
// are some misstakes done in above functions; exists...() and check(),
// or files to delete >= (more than or equal to) value of $careful,
// or permission issues at above call to PHP's unlink() function.
$i++;
if ($i >= $careful) { echo("overrun... at ".(string)$i."
"); break; }
}
// Output some info. If misstakes with permissions on files or folder
// delete character @ at @unlink() and watch for error output from PHP.
if (isset($n)) {
echo("Succeeded to delete ".(string)($n)." files.
");
} else {
if (isset($halt)) { $add = "Or it exist some permission issues."; }
echo("Nothing to delete. ".$add."
");
}
?>
Save above code in say "getridofsessintmp.php" or whatever.
And use it from i.e. the following below code:
Save below code in say "needtodeletesomefiles.php" or whatever.