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

glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

script to download AND delete a file...

Post by glennn3 »

i'm providing downloadable executables for several particularly ignorant clients who i cannot trust to be able to delete the file once downloaded (this is necessary) - i'd be very appreciative of some help with a script that would first download a file and then delete the same file...

thanks so much,
gn
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

uurrmm I dont believe that php can delete something client-side - it just doesnt seem right to me. What are they doing with these files? Are you sure that its something they need to download?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Why would someone want to download a file and then delete so others cannot download it for themselves. Something sounds wrong here dude!

*edit* Wooo everyone I changed my avatar for once...
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

magicrobotmonkey wrote:uurrmm I dont believe that php can delete something client-side - it just doesnt seem right to me. What are they doing with these files? Are you sure that its something they need to download?
what are you talking about ? a simple unlink() function can be ran to do so. i just need it to be done autmatically when the download is performed.

am i certain that it's something that they need to download? are you serious?

am i in the right forum...?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You wouldn't want PHP to do that on it's own - it would need some help from JavaScript - it is possible, but unreliable.

First, get the size of the file. Build a page with a JavaScript re-director pointing at the page with the 'delete file' PHP script. Work out how long it should take the download to complete, and add a percentage as a safety margin. Also use JavaScript to open a new window which starts the download.
When the time is up, the JavaScript redirects to the delete page, and the file is deleted.

You could use the php wait() command, but it would probably time out before the download is completed, depending, of course, on the size of the files vs connection.

How big are the files?
Last edited by Grim... on Fri May 28, 2004 10:49 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Glenn - no need to be like that - your first post was worded so it read like you wanted your clients to download the file then delete it from their local computer, not your server.
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

Joe wrote:Why would someone want to download a file and then delete so others cannot download it for themselves. Something sounds wrong here dude!

*edit* Wooo everyone I changed my avatar for once...
what sounds wrong with that? are you smoking something?

it's a private folder for each client that is available just to them for periodic software updates. they only need to download it once. if it stays in the folder then it'll simply confuse them.

IT'S WHAT MY CLIENT WANTS. is that confusing..?

perhaps i'm in the wrong forum. this must be the "We'll question your real needs" forum...
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Perhaps you should be a little more polite.

I've already answered your question.
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

Grim... wrote:Glenn - no need to be like that - your first post was worded so it read like you wanted your clients to download the file then delete it from their local computer, not your server.
i apologize - you're right - i had just read the inane post following yours and couldn't understand why my needs are being questioned. i see the confusion in my original post - yes, it needs to be dleted from the remote dir once downloaded.

sorry... :o)
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

Grim... wrote:Perhaps you should be a little more polite.

I've already answered your question.
i was apologizing to you - Joe's post was the ridiculous one - but i was writing that before you gave me the chance to read your follow up.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Anyway, IMO you're going about this the wrong way.

Why not use a database to hold information about the current version for each client, along with the appropriate links?

That way a flag could be set when the client click the download link, and a cron job at the end of each day could delete the files (as long as a good amount of time has passed since the client clicked the link).
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Dang, guy, you need to relax. I was trying to figure out just exactly what you where trying to do. Your poor use of English led me to interpret your cryptic post as you trying to use php to delete a file from the clients machine. In the future, you would do better to gently correct someone who is trying to help you.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Glenn: Your post was hardly verbose, was it?
glennn3 wrote:perhaps i'm in the wrong forum. this must be the "We'll question your real needs" forum...
And out of interest, yes - you did post in the wrong forum.
This thread should be in the PHP - Code section.
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 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.
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

Post by glennn3 »

thanks for your time - here's what this is :

some financial apps for car dealers that my client wrote in some archaic code that requires a new exe to simply update data. VERY small files. when they login they're taken to their own directory where the necessary update has been placed by my client. he wants the update to dissapear, right after they download it so that they're not confused (these are car salesmen, not PC users...) - the simple website and script i built in a day for 300 dollars and i think implementing a db would be a little overkill.

i had put in a unlink() scipt to be called by the user but he doesn't even truat the salesmen to do two more steps (clicking a second file) so he wants me to make the file go away when they download it.

i know, it sounds stupid, but he's my client.

there's got to be a way to download a file in php and then run an unlink() in the same function... ? unreliable...?

thanks again
Post Reply