0

I have a custom PHP class that stores a large amount of data (serialized arrays in a text file) for use with the program. I've set up a garbage collection that every time any user runs the program, it parses through the temp director to delete any files older then one hour. After which the file will be recreated by the program for use.

However, what I would really like is to delete the files when the user presses an input button on my form that closes the page. I've tried passing a JSON object with JavaScript/AJAX using POST, but I couldn't get the variable where it needed to be. By that I mean I have a main page that has a form for user input that further populates a page in an iframe based on user input. The iframe is where I needed the JSON object to go. I was attempting to set a Boolean flag to true (i.e. dead=true) and have my garbage collection check for "dead" and then unlink/delete the file.

At this point I'm just looking for any ideas on how to proceed. I have limitations that prevent me from changing basic framework (i.e. can't install or use jQuery or any other JavaScript LIB).

I've seen a lot of similar questions, but none that have a method I can use. Other solutions use a timed garbage collection, which I'm already doing. I was hoping new techniques or solutions have come to light. I'm not looking for code so much as new techniques to handle this.

KatieK
  • 13,586
  • 17
  • 76
  • 90
Iron3eagle
  • 1,077
  • 7
  • 23
  • Also if anyone knows a better way to handle MASS amounts of data in php please share. Maybe I'll abandon the temp files. Temp file sizes are atleast 10mb+ in most cases, with multiple files per user. – Iron3eagle Oct 26 '12 at 14:53
  • i think pos dup of http://stackoverflow.com/questions/4594396/how-can-i-call-a-server-method-when-the-user-close-the-page and http://stackoverflow.com/questions/1756963/how-to-delete-a-file-whenever-user-leaves-page – NullPoiиteя Oct 26 '12 at 14:54
  • I saw those and thats the problem. All these have a solution of using a timed garbage collection. Which I'm already doing. Considering one was asked over a year ago and another three years ago. I was hoping new techniques or solutions have come to light. – Iron3eagle Oct 26 '12 at 14:57
  • 1
    Can you not just `unlink` the temp file in your button/form submit code?? The script must know a location to write to or read from, therefore it is given that you know the location of the temp file already. – PenguinCoder Oct 26 '12 at 15:45
  • I'm using OOP for this. So I have menu.php file another php file that gets the data and uses xml, and a bunch of html templates for the menu.php page. The objects that have the refrences to the files is stored in the xml page, not the menu page. However that does give me an idea to possible store the object in a session and refrence it at the menu page and unlink it there. Though that will require me to mix php into my html templates, still though I'll try it out Penguin! Sometimes I think too hard about things and make it harder then it is. – Iron3eagle Oct 26 '12 at 17:27
  • Also I really don't know the name of file per say. It's a unique random generated file name stored in a variable inside said object. But like you said if I store the object in a session I should be able to refrence it from the menu php page, and inbed unlink in a php block of my html template. I'll let you know if it works and I'll post if your answer works. (sure wish I could edit comments lol) – Iron3eagle Oct 26 '12 at 17:35

1 Answers1

0

You can just use cron along with a simple shell script that uses find to delete some files when they have not been used for (say) an hour. Sure the moral equivalent is possible in windows.

That should do the trick.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • I'll take a look at that option as well Ed, constant garbage collection would still be a step forward from doing it only when the page is ran. Thanks for the links as well! – Iron3eagle Oct 26 '12 at 17:41
  • @defaultNINJA - Does it matter if you have some temporary files that are not being used or never going to be used? Just tidy the workspace up once in a while will do the trick! – Ed Heal Oct 26 '12 at 17:45
  • It's not a MAJOR concern, but basicly it's house keeping. There could be a large number of people using this particular program, so the less disk space I utilize during X period of time the better. Worse case senerio I'll keep with my current garabage collection. Sad thing is right now my Dev Server is down so I'm waiting for it to come back up to try these new ideas. – Iron3eagle Oct 26 '12 at 19:36