Jump to content

Demoting A Normal File To A Temporary


boen_robot
 Share

Recommended Posts

Is there a way to make a certain file temporary, i.e. schedule it for deletion after the script ends?The script that needs to have it is a class... right now, I have a property that stores all temporary files' locations, and in the class' destructor, I unlink() them all. This works... most of the times. If the script fails with a fatal error for whatever reason (e.g. the user of the class causes an error by means other than using my class), the destructor is never activated, and therefore, the files remain.I haven't tried it, but I suspect the register_shutdown_function() function will have the same effect. Only the tmpfile() function never fails, since the cleanup on that is done internally by PHP (on the C layer). However, tmpfile() saves the file to an unsettable location (unless there is a way to dynamically set the system TEMP dir? AFAIK, doing so normally requires a reboot.), which is unacceptable for my class' purpose.I need to be able to write something to a file at a certain specified location, and have that file deleted after the script ends for whatever reason.Does anyone know of another way to create temporary files, or set up normal files to be deleted with or without a fatal error?[edit] Spoke too soon... just tried the register_shutdown_function() in an isolated environment. It does fire even on a fatal error, and what's even better - I can register an unlimited amount of them. Goodbye temporary file array! :) [/edit]

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...