script to download AND delete a file...

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

Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

PatrickG: Ah yes - bravo :)
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

patrikG wrote:If you want to use a custom session-handler (logging the session in the database) and use each client's session-id to name the particular file (e.g. "myfilename_adc74dc8a458dca586c7a52.pdf"). In the session garbage collection, you scan the clients' folders and delete all files that match "my_filename_".$session_id.".pdf".

That way you don't need to worry about timing - a session either times out or is terminated at which point your garbage collection comes into play - and: it's neat and generic.
good deal - thanks...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

If you want to avoid the database: are you using fopen to read the file to download before the fpassthru? If so, then you shouldn't be able to unlink it unless you've closed it with fclose.

So, in theory, you should be able to
1. open the file using fopen
2. initiate the download with fpassthru
3. after the download is finished, you close it with fclose and run a
while(!unlink($theFileName)){
}

Obviously, this is a quick and dirty solution... - haven't tried it, but it's something shouldn't be too far from the solution, if it doesn't work.

Edit: moved to php-code
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

yes, i don't want to mess with sessions in this Very cheap website.

originally the file was just being downloaded without php (just by clicking on it - he was miraculously able to teach them how to save it to their pc that way :)

i've never used fpassthru, so i'm unaware of it. i'm a novice at scripint; had no need to use fopen - i'll research the the fpassthru function, tho. that's probably what i need, and then, yes, just stick the unlink() at the end.

quick and dirty is just what i'm looking for. thanks P.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

Why don't you just make the PHP pass the file to the client, then when the PHP code has passed the last byte, delete the file? It works fine. No cron jobs. No hassle :-P
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

THAT's exactly what i'm talking about. just don't know the function for that - i've been trying

Code: Select all

$source="file.exe"; 
$dest="C:\test\file.exe"; 
copy($source_file, $dest_file); 
if (!copy($source, $dest)) { 
    print ("failed to copy $dest...\n"); 
}
but this obviously doesn't want to write to my local machine...

my noviceness is showing, i know...

thanks
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

Code: Select all

$fn="c:/path/to/file";

set_time_limit(0);

$fp=fopen($fn, "rb");
while (!feof($fp)) {
	echo fread($fp, 40960);
	flush();
}
fclose($fp);

unlink($fn);
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

thanks, but we won't want the file to open - it's an exe... isn't there a function that will just open a save as dialog box? maybe this shouldn't be done in php, like Grimm said...

thanks
slimsam1
Forum Commoner
Posts: 49
Joined: Wed Aug 21, 2002 12:20 am

Post by slimsam1 »

Why not:


send the headers
use readfile to send data to browser
unlink same file we just used readfile on

?
slimsam1
Forum Commoner
Posts: 49
Joined: Wed Aug 21, 2002 12:20 am

Post by slimsam1 »

Code: Select all

<?php
$fn="c:/path/to/file";

set_time_limit(0);

$fp=fopen($fn, "rb");
while (!feof($fp)) {
   echo fread($fp, 40960);
   flush();
}
fclose($fp);

unlink($fn);

?>
glennn3 wrote:thanks, but we won't want the file to open - it's an exe... isn't there a function that will just open a save as dialog box? maybe this shouldn't be done in php, like Grimm said...

thanks

Did you try that already? I think the download box depends on browser settings and the headers sent to the browser.
Post Reply