CodeIgniter Forums
Delete file after download - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Delete file after download (/showthread.php?tid=79320)



Delete file after download - MatheusCastro - 05-27-2021

I have a situation that should download and delete the file. Is there any possibility?

I'm using this function in response:
PHP Code:
return $response->download($name$data); 

Anything like that, for example:
PHP Code:
// Other framework
return response()->download($filePath$fileName$headers)->deleteFileAfterSend(true); 

This is the bad solution?

PHP Code:
Controller:

// Set header content disposition file
header("Content-Disposition: attachment; filename=" $fileName);

// Force download writer to output
$writer->save('php://output');

// Remove file after download
unset($filePath);

// Break request to not corrupt the file
exit(); 

Im using PHP Spreedsheet library.


RE: Delete file after download - InsiteFX - 05-28-2021

To delete a file you use unlink(path/filename);


RE: Delete file after download - MatheusCastro - 05-28-2021

(05-28-2021, 02:35 AM)InsiteFX Wrote: To delete a file you use unlink(path/filename);

Yes, okay! But the main question is whether there is any way to use the download with the response and delete it afterwards? Or if the solution would be the way I posted below not to use response->download.


RE: Delete file after download - InsiteFX - 05-28-2021

This may help not sure.
CodeIgniter 4 User Guide - Uploaded Files


RE: Delete file after download - demyr - 05-29-2021

You can trigger a delete function via jquery / ajax when you click the download button.