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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bhall
Forum Newbie
Posts: 1
Joined: Fri Apr 22, 2005 9:54 am

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

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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().
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;-)
Post Reply