Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a file after user download prompt

I think this is pretty simple. My program will create an Excel file on the server using Automation. After that, I want to force the user to choose whether or not to download it, then, regardless of the choice, delete the file so it won't just take up space. Any ideas on how I could accomplish this?

like image 489
Matthew Jones Avatar asked Dec 30 '22 16:12

Matthew Jones


2 Answers

You could read the file into an input buffer then delete the file and send the buffer to the user instead of the actual file.

like image 119
corymathews Avatar answered Jan 01 '23 06:01

corymathews


It's hard to know when the user has actually finished downloading the file, and you don't want to delete it before doing so, therefore my advice is to delete files older than a certain amount of time, for example > 24 hours.

It's hard to determine if the user has finished downloading the file because the browser doesn't transmit this information, and other than packet sniffing the network data and configuring in detail your webserver to manage its open connections, and actually believing it won't make errors, there's no simple way.

It also depends how you're reading the file and its actual size, for example going line by line to output to the browser will save memory, but it'll make the code dependant on the file's existence, while reading the file's content to some kind of buffer will allow the webserver to well, serve it, but depending on the number of visitors, the file size, and average download speed, you might have at any time a webserver with dozens of GB files in memory.

It's just easier to have enough space for a few days worth of data and delete files every now and then.

like image 37
inerte Avatar answered Jan 01 '23 05:01

inerte