<?php
    require_once dirname(__FILE__) .  '/../php_only_scripts/global_functions.php';

    $path = dirname(__FILE__) . '/../pdf_templates/temp/';
    
    // Get the directory handle
    $handle = opendir($path);
    
    // Determine if this is a path
    if ($handle) {
        // Loop through the files in here
        while (false !== ($file = readdir($handle))) { 
            // Determine how relative it is to this directory
            if ($file != "." && $file != "..") { 
                // Get the value of the file last modified
                $filelastmodified = filemtime($path . $file);

                // Determine if it is older than 24 hours. (24 hours in a day * 3600 seconds per hour)
                if(((time() - $filelastmodified) > 24*3600)){
                    // Is it a directory
                    if (is_dir($path . DIRECTORY_SEPARATOR . $file) && !is_link($path . DIRECTORY_SEPARATOR . $file))
                        // Call the recursion again on this directory inside of this directory
                        removeDirectory($path . DIRECTORY_SEPARATOR . $file);
                    else
                        // Remove the file
                        unlink($path . DIRECTORY_SEPARATOR . $file);
                }
            }
        }

        // Close the directory
        closedir($handle); 
    }
    
    // Exit this code
    exit;
?>