Automatically delete a temporary directory when destroyed
mainIf you want the directory to be automatically deleted when the TemporaryDirectory object is no longer referenced (e.g., when it goes out of scope or is unset()), call deleteWhenDestroyed() before creating it. This removes the need to manually call delete().
function handleTemporaryFiles()
{
$temporaryDirectory = (new TemporaryDirectory())
->deleteWhenDestroyed()
->create();
// ... use the temporary directory
return; // The directory is automatically deleted here
}
handleTemporaryFiles();