script to download AND delete a file...
Moderator: General Moderators
good deal - thanks...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.
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
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
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.
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.
THAT's exactly what i'm talking about. just don't know the function for that - i've been trying
but this obviously doesn't want to write to my local machine...
my noviceness is showing, i know...
thanks
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");
}my noviceness is showing, i know...
thanks
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);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.