Page 1 of 1

Write a file from a form, download the file, and then delete

Posted: Fri Apr 22, 2005 10:03 am
by bhall
I'm using three pieces of code that I found online. The first and last parts are from a tutorial on how to write a file to the server and how to delete a file.

And then I'm using another piece for the user to download the file.

Basically, it's a form that will be used a lot to dynamically create html files. But since I don't want a thousand files left on my server, I'm creating the file, then deleting it when they're done.

So far, everything works fine. The last step is to have the user re-directed back to the form after they've downloaded the page.

Problem though is timing, I think I've got three files... the form (html), the first php page that writes the file and links to the php file that downloads (and then deletes) the file. By "download", I mean, pops up the window for them to save the file.

The file I'm using for the download is called "force-download.php" - found it here:
http://www.elouai.com/force-download.php

It's all just a txt file, am I over-complicating things?

Posted: Fri Apr 22, 2005 2:05 pm
by onion2k
Change it so that whenever a user tries to download a file it checks, and deletes, any files that are older than 24 hours using a combination of filemtime() and unlink().

Posted: Fri Apr 22, 2005 7:21 pm
by Chris Corbyn
Why do you need to save it to disk?

There's another way. If you simply have the data stored as a string variable (which I assume you fwrite() to the server and then download the file) you can output it straight to a file. Simply pass the data via POST or sessions to another script which outputs the appropriate headers and echo's the string into the page... the file (that isn't even there in effect) will download to the browser.

If you want me elaborate I'll show an example ;-)