/    Sign up×
Community /Pin to ProfileBookmark

Create a temp file for download, then delete

Hello!
Well i’ve worked my way through making a system for dynamic content creation and now I need to make it available for download in one single file. But it would be a waste of space if I create a file every time a user wants to download the info so I am thinking in a temp file, after the user has downloaded it then just delete it.
I know how to create a text file (I think) but im not sure of how to set the deletion of it and im not exactly talking about the unlink function.
Suppose a user runs the search and gets the results and clicks the download button but goes for a cup of coffee, if I name the temp file always the same then another user could run a search in the site and overwrite the temp file before the first user actually downloads it and that is not cool.
To fix this I will name each temp file with a ‘unique’ number (based on seconds and time stuff) but in doing so I would surely need to delete them after download.

How do you think I could work this out?
thanks!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsJan 05.2006 — Into your main application (or a cron tab) write a script that gets the time from each temp file sees if it's a day later or a couple hours or something and then unlinks it. Alternatively, you could just drop saving the files and dump them to the browser which will then download them, without using any space or extra bandwidth.
Copy linkTweet thisAlerts:
@purefanauthorJan 06.2006 — I liked your second option better so I got to work:

I found out how to get the last modification date of a file but I still havent managed a way of retrieving every .PGN file in the current folder.

If you could point me to where could I find such information that'd be great!

Thanks SpectreReturns!
Copy linkTweet thisAlerts:
@balloonbuffoonJan 06.2006 — [code=php]
//this will give an array of all the .PGN files in the current directory
$files = glob("*.PGN");
[/code]
Copy linkTweet thisAlerts:
@purefanauthorJan 06.2006 — I got it!

This next piece of code will search all files in current folder and then delete those who are older than 5 minutes and with an extension PGN

Thanks for your help!!
[code=php]
$dir = dirname(__FILE__);

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {

$EsPGN = substr_count($file, '.pgn');
if(!empty($EsPGN))
{
//Buscar PGNs viejos

$filedate = date("F d Y",filemtime("$file"));

if((date('dHs')-$filedate)>300){
unlink($file);
}//if older 5 minutos
}//esPGN
}
closedir($dh);
}
}
[/code]
Copy linkTweet thisAlerts:
@balloonbuffoonJan 06.2006 — [code=php]
//this will give an array of all the .PGN files in the current directory
$files = glob("*.{PGN,pgn}", GLOB_BRACE);
foreach ($files as $file) {
$filedate = date("F d Y",filemtime($file));

if((date('dHs')-$filedate)>300){
unlink($file);
}//if older 5 minutos
}
[/code]

More condensed version for ya.

--Steve
Copy linkTweet thisAlerts:
@purefanauthorJan 06.2006 — More condensed version for ya.[/quote]
...The difference between those who know (balloonbuffoon) and those who are learning (Me!)

Thanks a lot balloonbuffoon!! it works like I want it!!

?
Copy linkTweet thisAlerts:
@balloonbuffoonJan 06.2006 — No problem, its always nice to save space in your code!

--Steve
×

Success!

Help @purefan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...