I am developing a web-based user interface that plots some data from a database and displays it to the user. There are, literally, millions of different graphs that may be generated, so my idea is to dynamically generate the graph, save it to a temporary file, serve the web page and finally remove the image after a certain period of time.
What I would like to do, thus, is to store the paths to the already generated images in $_SESSION so that the files can be deleted when the session is destroyed. Is there a way in PHP to execute a given function (which in this case would be in charge of deleting the aforementioned files) when the session is destroyed, whenever that happens?