0

Hey my app creates files on server that are shown to users right away in html element called iframe. So far I have been storing all files into same folder, overwriting old ones each time user requested new one. Problem is that if more than one user tried to generate file at once, one of them might not get the right file, or no file at all. I guess i should create random temp folder for each generation of files. That would work, but my server might get filled up really quickly in like 100 generations of files. I need to delete this random temp folder once user is done loading up file in iframe, but how do I call php function on load?
Any other alternative method/idea, would help me alot. How do others handle such problems?

niproblema
  • 71
  • 1
  • 10
  • Are you saying if someone uploads something called 'myfile.jpg' and someone else uploads the same named file, it will overwrite it? Is that what you mean when you say "..one of them might not get the right file, or no file at all"? – Rasclatt Oct 17 '14 at 19:31
  • This is why most people will store both the default name and some hashed name of the file. this could be something like `md5(date('Y-m-d H:i:s'))` And then make this the actual file name so there won't be any duplicate files – SuperDJ Oct 17 '14 at 19:34
  • @Rasclatt My script generates files with same name for every user. I could name each file differently or put it into separate temp folder, but then I need a good method to get rid of tthese temp files, or they might use up all space on my server... – niproblema Oct 17 '14 at 19:38
  • I think you should just upload the file as suggested by @SuperDJ. Maybe modified a bit further like: `md5(date('Y-m-d H:i:s').$_SESSION['username'])` or something just on the off chance that someone uploads at the exact same second... – Rasclatt Oct 17 '14 at 19:41
  • @Rasclatt Alright, but how do i get rid of the file once user sees it in his browser. File has no longer any use for anyone and it is taking up space on server – niproblema Oct 17 '14 at 19:45
  • @Dinei A. Rockenbach has a good plan I would say. – Rasclatt Oct 17 '14 at 19:46

1 Answers1

1

I would create one single temp folder, and generate random file names (md5, GUID, don't matter).
The same script that generate the file on the folder to visualization, before the generation itself, should check all the files on the folder, excluding all the files which were generated >= 1 day ago.

Check the filemtime function and this question if you have any doubts about how to get the file creation/modification date.

Community
  • 1
  • 1
Dinei
  • 2,451
  • 3
  • 25
  • 49
  • 1
    Thanks m8 gr8 idea. Actually 10 min file life span should be enought, since I need to save on script running time. In one day i could get thousands of files, each few mb. – niproblema Oct 17 '14 at 19:54
  • If the performance of your script is a critical point, you could also make another script to clean the old files, and use the linux crontab (or windows task scheduler) to call it every few minutes. – Dinei Oct 17 '14 at 20:08