Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

delete_temp_files()

Description

Delete old files and folders from tempo directory based on the configured $purge_temp_folder_age value
Affects filestore/tmp, $storagedir/tmp or the configured $tempdir directory

Parameters

ColumnTypeDefaultDescription
void { :

Location

include/file_functions.php lines 608 to 698

Definition

 
function delete_temp_files(): void
{
    if (
$GLOBALS["purge_temp_folder_age"] === 0) {
        
// Disabled
        
return;
    }
    
// Set up array of folders to scan
    
$folderstoscan = [];
    
$folderstoscan[] = get_temp_dir();
    
    
$modified_folderstoscan hook("add_folders_to_delete_from_temp""", array($folderstoscan));
    if (
is_array($modified_folderstoscan) && !empty($modified_folderstoscan)) {
        
$folderstoscan $modified_folderstoscan;
    }

    
// Set up array of folders to exclude
    
$excludepaths = [];
    if (isset(
$GLOBALS["geo_tile_cache_directory"])) {
        
$excludepaths[] = $GLOBALS["geo_tile_cache_directory"];
    } else {
        
$excludepaths[] = get_temp_dir() . "tiles";
    }
    if (
DOWNLOAD_FILE_LIFETIME $GLOBALS["purge_temp_folder_age"]) {
        
$excludepaths[] = get_temp_dir(false"user_downloads");
    }

    
// Set up arrays to hold items to delete
    
$folderstodelete = [];
    
$filestodelete = [];

    foreach (
$folderstoscan as $foldertoscan) {
        if (!
file_exists($foldertoscan)) {
            continue;
        }
        
$foldercontents = new DirectoryIterator($foldertoscan);
        foreach (
$foldercontents as $object) {
            if (
time() - $object->getMTime() > $GLOBALS["purge_temp_folder_age"] * 24 60 60) {
                
$tmpfilename $object->getFilename();
                if (
$object->isDot()) {
                    continue;
                }
                foreach (
$excludepaths as $excludepath) {
                    if (
                        (
$tmpfilename == $excludepath)
                        || 
strpos($object->getRealPath(), $excludepath) == 0
                    
) {
                        continue 
2;
                    }
                }
                if (
$object->isDir()) {
                    
$folderstodelete[] = $foldertoscan DIRECTORY_SEPARATOR $tmpfilename;
                } elseif (
$object->isFile()) {
                    
$filestodelete[] = $foldertoscan DIRECTORY_SEPARATOR $tmpfilename;
                }
            }
        }
    }

    foreach (
$folderstodelete as $foldertodelete) {
        
// Extra check that folder is in an expected path
        
if (strpos($foldertodelete$GLOBALS["storagedir"]) === false
            
&& strpos($foldertodelete$GLOBALS["tempdir"]) === false
            
&& strpos($foldertodelete'filestore/tmp') === false
        
) {
            continue;
        }

        
$success rcRmdir($foldertodelete);
        if (
'cli' == PHP_SAPI) {
            echo 
" - deleting directory " $foldertodelete " - " . ($success "SUCCESS" "FAILED")  . PHP_EOL;
        }
        
debug(" - deleting directory " $foldertodelete " - " . ($success "SUCCESS" "FAILED"));
    }

    foreach (
$filestodelete as $filetodelete) {
        
// Extra check that file is in an expected path
        
if (strpos($filetodelete$GLOBALS["storagedir"]) === false
            
&& strpos($filetodelete$GLOBALS["tempdir"]) === false
            
&& strpos($filetodelete'filestore/tmp') === false
        
) {
            continue;
        }

        
$success try_unlink($filetodelete);

        if (
'cli' == PHP_SAPI) {
            echo 
" - deleting file " $filetodelete " - " . ($success "SUCCESS" "FAILED")  . PHP_EOL;
        }
        
debug(" - deleting file " $filetodelete " - " . ($success "SUCCESS" "FAILED"));
    }
}

This article was last updated 31st March 2025 13:05 Europe/London time based on the source file dated 25th March 2025 10:50 Europe/London time.