Reply
Old 04-15-2008, 02:56 PM fclose()
Gilligan's Avatar
Dead Like Me

Posts: 1,597
Name: Stefan
Location: London, UK
Is it possible to fclose a file after 30 minutes of time?

I'm using it to create a temporary file, then the user can download it, and hoping that the file can be deleted after about 30 mins, otherwise how could the user download it, unless they were quicker than the browser.

EDIT

Will unlink() do this?

Last edited by Gilligan : 04-15-2008 at 03:09 PM.
Gilligan is online now
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-15-2008, 04:20 PM Re: fclose()
tripy's Avatar
Fetchez la vache!

Posts: 1,958
Name: Thierry
Location: In the void
I don't think no...
As the PHP model (like any "web" server side language) is a fire and forget, you can only keep your file open the time the page load and the PHP engine is running.

Just a side note here, windows prevents you from deleting an opened file; Linux/Unix is not.
Even if someone is editing a file, someone else can do a delete of that file, and nobody will see anything.
unlink is nothing else than the delete command.

For what you need to do, you will need to wrap the access around a PHP access method, and it's that PHP file that will restrict the access.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 04-15-2008, 04:44 PM Re: fclose()
Gilligan's Avatar
Dead Like Me

Posts: 1,597
Name: Stefan
Location: London, UK
So I can't set a timer for the file to exist? 30 minutes and then delete.

Maybe there's some kind of if else statement that will work.

PHP Code:
if (timepassed 30 minutes || user exits page) {
  
fclose($handle)

anything along the lines of that??
Gilligan is online now
Reply With Quote
View Public Profile
 
Old 04-15-2008, 07:10 PM Re: fclose()
tripy's Avatar
Fetchez la vache!

Posts: 1,958
Name: Thierry
Location: In the void
You will need to store the access time somewhere then, and check periodically.
Or to set an AT job for it, but as each process is fired and die in the page construction, you have no way to monitor it all the time.

In fact, yes, there is a way, but out of the scope of a web site.
You can write threaded with the pcntl [ http://www.php.net/manual/en/intro.pcntl.php ] functions (I did this in the paste), and run it as a service, but this is totally different than web development.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 04-15-2008, 08:09 PM Re: fclose()
Gilligan's Avatar
Dead Like Me

Posts: 1,597
Name: Stefan
Location: London, UK
Thanks, I just learnt a bit more about creating / writing files and so I done this instead

fopen
fwrite
fclose

headers (make them download)

unlink (delete)

Works a treat May not be 30 mins, but the page is deleted instantly, and a refresh writes the file again

Last edited by Gilligan : 04-15-2008 at 08:11 PM.
Gilligan is online now
Reply With Quote
View Public Profile
 
Old 04-15-2008, 08:13 PM Re: fclose()
mgraphic's Avatar
Truth Seeker

Posts: 2,311
Name: Keith Marshall
Location: West Hartford, CT
You can use a function that checks the modification time of the file...

if (filemtime('path/of/filename.txt') > time() + (60*30)) unlink('path/of/filename.txt');
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-16-2008, 02:49 AM Re: fclose()
tripy's Avatar
Fetchez la vache!

Posts: 1,958
Name: Thierry
Location: In the void
Quote:
You can use a function that checks the modification time of the file...
Yep, it's true, but just remember that this function use cached results.
If you want to have the up to date infos, call a clearstatcache() [ http://www.php.net/manual/en/functio...rstatcache.php ] before
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 04-16-2008, 06:10 AM Re: fclose()
mtishetsky's Avatar
Super Spam Talker

Posts: 946
Location: Volendam, Netherlands
crontab, find /path/to/files -type f -cmin +30 -delete
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 04-16-2008, 07:01 AM Re: fclose()
Gilligan's Avatar
Dead Like Me

Posts: 1,597
Name: Stefan
Location: London, UK
I used to think fclose deleted the file, now I realise unkink does
Gilligan is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to fclose()
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.18493 seconds with 13 queries